我有这个:
char* original = "html content";
并想插入一个新的
char* mycontent = "newhtmlinsert";
进入之前的“原文”</body>
标签在“原始”中。
我的新原版现在是:
char* neworiginal = "html content before </body>" + "newhtmlinsert" + "html content after </body>";
基本上我想获取 char* 原件并将其转换为 char* 新原件,其中包含原始内容以及我在之前添加的新内容</body>
在“原始html内容”中
这是修改后的代码,我仍然需要一些帮助:
* 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, 0, 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,data_len - ending );
我如何执行最后一条语句:memcpy(newdata,data,data_len - ending );
i need to copy the remainder of the data from my char* data beginning from an
最后......我如何正确计算memcpy中的“结束”参数?
这是使用字符串的 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