I am trying to write some short C code that will find the word "Applicat" and replace that entire line with something else, not just the word. For example, I have a test.txt file that says:
Name: test
Applicat: something
Date: today
My current code would find the word "Applicat," and replace it with "Applicat: ft_link," but the "something" is still there. How can I make it look like this:
Name: test
Applicat: ft_link
Date: today
?
Or is there an easier way to do this?
Thanks in advance!
Here is the main part of my code:
char buffer[512]; while (fgets(buffer, sizeof(buffer), input) != NULL) { static const char text_to_find[] = "Applicat:"; static const char text_to_replace[] = "Applicat: ft_link"; char *pos = strstr(buffer, text_to_find); if (pos != NULL) { char *temp = calloc( strlen(buffer) - strlen(text_to_find) + strlen(text_to_replace) + 1, 1); memcpy(temp, buffer, pos - buffer); memcpy(temp + (pos - buffer), text_to_replace, strlen(text_to_replace)); memcpy(temp + (pos - buffer) + strlen(text_to_replace), pos + strlen(text_to_find), 1 + strlen(buffer) - ((pos - buffer) + strlen(text_to_find))); fputs(temp, output); free(temp); }