I need to read a string from stdin, and then delete all the occurrences of that string in a file. I must overwrite the original file. Below I post my source code so far. Its problems are that I do not know what stop condition I should add for the while
loop, and also, how do I overwrite the original file? (As you can see, I tried to add all the words that are not equal to the given string in a separate file).
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp, *fp_out;
char s[50], del[50];
fp = fopen("file_in", "r");
fp_out = fopen("file_out", "a");
fgets(del, 50, stdin);
fgets(s, 50, (FILE *)fp);
while(s != EOF) //I know that this does not work, what condition should I add?
{
fgets(s, 50, (FILE *)fp);
if(strcmp(s, del) != 0)
fprintf(fp_out, "%s ", s);
}
fclose(fp);
fclose(fp_out);
return 0;
}
I mention that I also tried to add while(s != NULL)
but this created a 592 MB text file, containing all the words in my input text file.