修改C中的char *字符串

时间:2011-04-14 18:47:43

标签: c++ c

我有这个:

char* original = "html content";

想要插入新的

char* mycontent = "newhtmlinsert";

进入“原始”中</body>标记之前的“原始”。

我的新原始现在是:

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

基本上我想要一个char *原始并将其转换为char * neworiginal,其中包含原始内容以及我在“原始html内容”中</body>之前添加的新内容

这是修改后的代码,我仍然需要一些帮助:

* 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

6 个答案:

答案 0 :(得分:4)

假设char* original由两部分组成,一部分从0开始,而另一部分(之后的html内容)从x开始,您可以使用strcatmemcpy

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

答案 1 :(得分:1)

您需要使用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()的边界变体。

#include <string.h>

char *strncat(char *restrict s1, const char *restrict s2, size_t n);

确保附加字符串的缓冲区有足够的空间不会导致缓冲区溢出。

答案 2 :(得分:0)

C ++没有char *字符串的+运算符。你需要使用std :: string,即

std::string neworiginal = "html content before </body>";
neworiginal += "newhtlminsert";
neworiginal += "..."

答案 3 :(得分:0)

查看strstrstrcatother cstring/string.h functions

确保您的char数组足够大以容纳连接字符串。比如,您可能想要执行以下操作:

char neworiginal[1024];

答案 4 :(得分:0)

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;
}

这有点难看,但它完成了工作:)

答案 5 :(得分:0)

尝试修改字符串文字的内容会导致未定义的行为。

您将需要分配一个目标缓冲区(作为自动变量或使用malloc),该缓冲区足够大以容纳最终字符串加上0终结符。

此外,您可能希望使用sprintf让生活更轻松,例如

sprintf(result, "%s before %s - %s - %s after %s", original, 
    tag, mycontent, original, tag);
相关问题