本文介绍了修改在C的char *字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

 的char *原=html内容;

和要插入一个新的

 的char *为myContent =newhtmlinsert;

进入原上述前刚< / BODY方式> 中的原始标签

我的新原单现在是:

 的char * neworiginal =HTML内容之前和LT; /身体GT; +newhtmlinsert+HTML内容后< /身体gt;中;

基本上我要带一个char *原单,并将其转换成有我在℃加入原来的内容加上新的内容一个char * neworiginal; / BODY> 在原始HTML内容

下面是修改code,我还是需要一些帮助:

  *原始数据* /
    数据= _msg_ptr->的buff();
    DATA_LEN = _msg_ptr-> DATALEN();
/ *的&LT位置; /身体GT;在原来的 *​​/    字符* =插入的strstr(数据,< /身体GT;);/ *新缓冲区的字符串的长度* /    INT长度= strlen的(数据)+ strlen的(ad_content);
    newdata =(字符*)malloc的(的sizeof(char)的*长度);
    memset的(newdata,0,长度);/ *复制原始数据高达< /身体GT;到newdata * /    的memcpy(newdata,数据,插入数据);/ *现在加上ad_content * /
    strcat的(newdata,ad_content);/身体GT; / *来自以下的复制数据;原始的字符串(数据)的一端插入newdata * /    的memcpy(newdata,数据,DATA_LEN - 结尾);

我如何落实的最后一条语句:的memcpy(newdata,数据,DATA_LEN - 结尾);

 我需要在剩下的数据从我的char *复制数据从开始

尽头......我如何正确计算中的memcpy的收官参数?

下面是使用字符串的C ++版本

 的char *插入=的strstr(_in_mem_msg_ptr->的buff(),< / BODY>); //获取指针到< / BODY>
字符串ad_data =字符串(_in_mem_msg_ptr->的buff(),插入 - _in_mem_msg_ptr->的buff()); //插入_in_mem_msg_ptr-&GT的一部分;)在&lt之前的buff(; /身体GT;
ad_data.append(ad_content); //添加新的HTML内容
ad_data.append(_in_mem_msg_ptr->浅黄色(),插入 - _in_mem_msg_ptr->浅黄色(),_ in_mem_msg_ptr-> DATALEN()); // _in_mem_msg_ptr-的制造&gt剩余;从浅黄色()并且包括与所述; /身体GT;到最后


解决方案

假设的char *原是由两部分组成,一是从0开始,而其他(HTML内容之后)开始于 X 您可以使用 strcat的的memcpy

  INT长度= strlen的(原)+ strlen的(newcontent)+1;
字符* neworiginal =的malloc(sizeof的(char)的*长度);
memset的(neworiginal,0,长度);
的memcpy(neworiginal,原来,X *的sizeof(字符));
strcat的(neworiginal,newcontent);
strcat的(neworiginal,原来+ X);

I have this:

char* original = "html content";

And want to insert a new

char* mycontent = "newhtmlinsert";

into the "original" above just before </body> tag in the "original".

My new orginal is now:

char* neworiginal = "html content before </body>" + "newhtmlinsert" + "html content after </body>";

Basically i want to take a char* orginal and convert it into a char* neworiginal which has the original content plus new content that i added before the </body> in the "original html content"

here is the modified code, i still need some help:

* 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 );

how do i implement the the last statement : memcpy(newdata,data,data_len - ending );

  i  need to copy the remainder of the data from my char* data beginning from an

the very end...how do i correctly compute the "ending" parameter in the memcpy?

here is the c++ version using strings

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
解决方案

Assuming that char* original is composed by two parts, one starts at 0 while the other (html content after) starts at x you can use strcat and memcpy:

int length = strlen(original)+strlen(newcontent)+1;
char *neworiginal = malloc(sizeof(char)*length);
memset(neworiginal, 0, length);
memcpy(neworiginal,original,x*sizeof(char));
strcat(neworiginal,newcontent);
strcat(neworiginal,original+x);

这篇关于修改在C的char *字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:06