I am writing a program that would delete the word entered from the keyboard from a text file (if it is there). After launch, the remaining words in the btest.txt file become one large line. How can you make the words in the output file be located in the same way as in the input file, only without the already deleted word?

#include <stdio.h> #include <string.h> int main(void) { char buf[512]; char word[128]; FILE *in_file = fopen("C:\\atest.txt", "r"); FILE *out_file = fopen("C:\\btest.txt", "w"); if(!in_file || !out_file) return -1; printf("Input word: "); scanf("%127s", word); while(!feof(in_file)) { fscanf(in_file, "%511s", buf); if(!strcmp(buf, word) ) continue; fprintf(out_file, "%s", buf); } fclose(in_file); fclose(out_file); } 
  • If you have words one by one in a line, then just write them down as fprintf(out_file, "%s\n", buf); . And also note that feof(in_file) does not work before, but after an unsuccessful attempt to read beyond the end of the file. - Harry
  • strstr() , first, secondly, it is not clear what (you) you can have separators between the "words". And thirdly, what kind of scanf() ?! - 0andriy

0