Modify a char* string in C
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 correctl开发者_Python百科y 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);
You need to use strcat()
for this problem.
Example =
/* 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;
}
Though you need to check the bounds so you can use the bounds variant of strncat()
.
#include <string.h>
char *strncat(char *restrict s1, const char *restrict s2, size_t n);
Make sure whatever buffer you are appending your string into has enough space to not cause a buffer overflow.
C++ doesn't have + operator for char * strings. You need to use std::string, i.e.
std::string neworiginal = "html content before </body>";
neworiginal += "newhtlminsert";
neworiginal += "..."
Take a look at strstr, strcat and other cstring/string.h functions.
Make sure your char
arrays are large enough to hold concatenated strings. Like, you may want to do the following:
char neworiginal[1024];
etc.
The string.h
function strcat
will concatenate two strings, however it will fail when there is not enough space for the new string. My solution is to make your own version of 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;
}
This is kinda ugly, but it gets the job done :)
Attempting to modify the contents of a string literal results in undefined behavior.
You will need to allocate a target buffer (either as an auto variable or by using malloc
) that's large enough to hold your final string plus a 0 terminator.
Also, you might want to use sprintf
to make life a little easier, such as
sprintf(result, "%s before %s - %s - %s after %s", original,
tag, mycontent, original, tag);
精彩评论