0

I have opened the file in "r+" mode and tried to use fputs to replace old_line (line) with new_line (updated_line)

char updated_line[] = "Hello This was ABCD";
fseek(fp, -(strlen(line))-1, SEEK_CUR); 
fputs(updated_line, fp);

Given txt file data :

Hello This is ABCD
Studying in XYZ College

Output:

Hello This was ABCD
udying in XYZ College

The two characters "St" are missing from next line. How do I rectify this ?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Vinay K
  • 1
  • 1
  • 2
    You can't just rewrite text in a text-file, unless the new text is *exactly* the same length as the existing text in the file. Instead you basically have to rewrite the whole file. Read it line by line, writing the lines into a new (temporary) file. When you reach the line you want to modify, write the modified line to the new file. Then rename the new file as the old original file. – Some programmer dude Mar 24 '23 at 08:51
  • @Someprogrammerdude Is correct, yet the -1 in `fseek(fp, -(strlen(line))-1, SEEK_CUR);` makes no sense. – chux - Reinstate Monica Mar 24 '23 at 10:35

0 Answers0