2

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?

4 Answers 4

4

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 memcpy or strncpy.

If the sizes are different, you could try something along the lines of:

char *buffer = malloc(size); // size should be big enough to store the whole final html code
find_pointer = strstr(sourcecode, text2find);
len = find_pointer - sourcecode;
memcpy (buffer, sourcecode, len);
memcpy (buffer + len, "king", 4);
memcpy (buffer + len + 4, find_pointer + 4, strlen(sourcecode) - len - strlen(text2find)); 
free(sourcecode);
sourcecode = buffer;
Sign up to request clarification or add additional context in comments.

Comments

3

Well, strcpy adds a 0-terminator. So although the remainder of the string remains in place, the standard string handling functions don't see it anymore because they stop at the 0-terminator. You can either manually overwrite it with a space or use memcpy instead of strcpy.

2 Comments

Yes, or that. Whichever you prefer.
This misses the much bigger problem that this strategy will not work unless both the search and replacement strings are the same length. Which, in this instance, they are not.
1

Replacing characters in a C string is painful, because you perform manipulations at a very low level, compared to, say, C++. You literally need to work out an algorithm for it!

First, observe that in-place replacement is not always possible: if the substring that you are replacing is shorter than the replacement, you would need to allocate more memory. It is easier to allocate the memory for the result either way, so you may proceed as follows:

  • Find the length of the string after the replacement. For that, you'd need to find the beginning and the end of the comment you're replacing, and do the math.
  • Next, you allocate a new chunk of memory for the result, and memcpy the source up to the replacement point into it.
  • Now you copy the replacement string, and finally the ending portion of the source into the result
  • Finally, you free the buffer of the source string, and return the result.

1 Comment

thank you. skyel said something similar and posted some code. cheers
0

First, you should use strncpy (or, better yet, strlcpy if available) because it actually performs bounds checking (i.e., it copies only a specified, supplied number of characters). Otherwise you may end up attempting to copy memory past the end of the destination string, resulting in undefined and potentially destructive behavior. Second, even if you were to use a function like strncpy or memcpy to avoid copying the terminating null character, your destination string would not be properly formatted because the string that you are trying to overwrite has a different length than the string that you're attempting to copy.

1 Comment

correct, it so happens that in html I just added a few spaces to make them the same length

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.