So, there is a text, for example: "I study at school", which is recorded in a file in the program. It is necessary to write the additional word "good" before the word school (in the same program). If I try to do this with the help of fprinf , then the word school is simply overwritten. How to do this without overwriting words, and shifting them to the right?

 #include <stdio.h> #include <stdlib.h> int main (void) { FILE *fp; if ((fp = fopen ("skul.txt", "w")) == NULL) { printf ("ERROR of open file skul.txt\n"); exit (EXIT_FAILURE); } fprintf (fp, "i lern in skul"); if (fclose (fp) != 0) { printf ("ERROR of exit from file skul.txt\n"); exit (EXIT_FAILURE); } if ((fp = fopen ("skul.txt", "r+")) == NULL) { printf ("ERROR of open file skul.txt\n"); exit (EXIT_FAILURE); } fseek (fp, 10L, SEEK_SET); fprintf (fp, "good "); if (fclose (fp) != 0) { printf ("ERROR of exit from file skul.txt\n"); exit (EXIT_FAILURE); } return EXIT_SUCCESS; } 
  • Copy to another file, pasting in the right place. Then rename the new file (to the original). - avp
  • 3
    The file does not expand the existing information. So just copy the data to an intermediate file and then rename the files. And yes, the school is spelled "school" and not "skul". - pepsicoca1

1 answer 1

There are 2 options, you can try changing the w to w + when writing, will give the opportunity to complement the file. but it will definitely work.