问题描述
|
我有这个:
char* original = \"html content\";
并想插入一个新的
char* mycontent = \"newhtmlinsert\";
进入“原始”中的</body>
标记之前的“原始”中。
我的新原著现在是:
char* neworiginal = \"html content before </body>\" + \"newhtmlinsert\" + \"html content after </body>\";
基本上,我想采用char *原始格式,并将其转换为char * neworiginal,它具有原始内容以及我在“原始html内容”中的“ 2”之前添加的新内容。
这是修改后的代码,我仍然需要一些帮助:
* original data */
data = _msg_ptr->buff();
data_len = _msg_ptr->dataLen();
/*location of </body> in the original */
char *insert = strstr(data,\"</body>\");
/*length of the new buffer string */
int length = strlen(data)+strlen(ad_content);
newdata =(char*)malloc(sizeof(char)*length);
memset(newdata,length);
/*copy the original data upto </body> into newdata*/
memcpy(newdata,data,insert-data);
/*Now add the ad_content */
strcat(newdata,ad_content);
/*copy the data from </body> to end of original string(data) into newdata */
memcpy(newdata,data_len - ending );
我该如何执行最后一条语句:memcpy(newdata,data_len - ending );
i need to copy the remainder of the data from my char* data beginning from an
最后...如何正确计算memcpy中的\“ ending \”参数?
这是使用字符串的C ++版本
char *insert = strstr(_in_mem_msg_ptr->buff(),\"</body>\");//get pointer to </body>
string ad_data = string(_in_mem_msg_ptr->buff(),insert - _in_mem_msg_ptr->buff()) ;//insert the part of _in_mem_msg_ptr->buff() before the </body>
ad_data.append(ad_content); //add the new html content
ad_data.append(_in_mem_msg_ptr->buff(),insert- _in_mem_msg_ptr->buff(),_in_mem_msg_ptr->dataLen()); //remainder of _in_mem_msg_ptr->buff() from and including </body> to the end
解决方法
假设ѭ9由两部分组成,一个从0开始,而另一个(后接html内容)从ѭ10开始,则可以使用
strcat
和memcpy
:
int length = strlen(original)+strlen(newcontent)+1;
char *neworiginal = malloc(sizeof(char)*length);
memset(neworiginal,length);
memcpy(neworiginal,original,x*sizeof(char));
strcat(neworiginal,newcontent);
strcat(neworiginal,original+x);
,您需要使用strcat()
解决此问题。
例子=
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,\"these \");
strcat (str,\"strings \");
strcat (str,\"are \");
strcat (str,\"concatenated.\");
puts (str);
return 0;
}
虽然您需要检查边界,但是您可以使用strncat()
的bounds变体。
#include <string.h>
char *strncat(char *restrict s1,const char *restrict s2,size_t n);
确保将字符串追加到的任何缓冲区都具有足够的空间,以免引起缓冲区溢出。
,C ++没有char *字符串的+运算符。您需要使用std :: string,即
std::string neworiginal = \"html content before </body>\";
neworiginal += \"newhtlminsert\";
neworiginal += \"...\"
,看一下strstr,strcat和其他cstring / string.h函数。
确保您的char
数组足够大,可以容纳串联的字符串。喜欢,您可能需要执行以下操作:
char neworiginal[1024];
等等
,string.h
函数strcat
将连接两个字符串,但是如果没有足够的空间容纳新字符串,它将失败。我的解决方案是制作您自己的strcat
版本:
char* myStrCat(const char* str1,const char* str2)
{
char* result;
char* itr1;
char* itr2;
if (str1 == NULL || str2 == NULL)
{
return NULL;
}
result = (char*)malloc(sizeof(char) * (strlen(str1) + strlen(str2) + 1));
itr1 = result;
itr2 = (char*)str1;
while (*itr2 != \'\\0\')
{
*itr1 = *itr2;
itr1++;
itr2++;
}
itr2 = (char*)str2;
while (*itr2 != \'\\0\')
{
*itr1 = *itr2;
itr1++;
itr2++;
}
*itr1 = \'\\0\';
return result;
}
这有点丑陋,但是可以完成工作:)
,尝试修改字符串文字的内容会导致未定义的行为。
您将需要分配一个足够大的目标缓冲区(作为自动变量或使用malloc
)以容纳您的最终字符串和一个0终止符。
另外,您可能想要使用sprintf
来使生活更轻松一些,例如
sprintf(result,\"%s before %s - %s - %s after %s\",tag,mycontent,tag);