I’ve copied an HTML file into an array using the following code:
fseek(board, 0, SEEK_END);
long int size = ftell(board);
rewind(board);
char *sourcecode = calloc(size+1, sizeof(char));
fread(sourcecode, 1, size, board);
Now my goal is to replace a certain comment in the array with the already defined char string ‘king’. E.g.
< html code>< !comment>< more html code>
to
< html code>king< more html code>
Im using the following code:
find_pointer = strstr(sourcecode, text2find);
strcpy(find_pointer, king);
printf("%s", sourcecode);
where text2find = “< !comment>”;
however when I print, it is evident that all my characters past ‘king’ have been erased.. as if it automatically added a terminating character. How can i fix this so < more html code> remains in place?
EDIT:::::
I used strncpy and set a number of characters such that the terminating character was not added. is this the best method?
You basically can’t do that, unless the stuff you want to replace is exactly the same size. In which case you can use either
memcpyorstrncpy.If the sizes are different, you could try something along the lines of: