When you open in append mode, the file pointer is returned to the end of file before every write. You can reposition the pointer with fseek
for reading, but as soon as you call a function that writes to the file, the pointer goes back to the end of file.
Or, putting it another way, to prevent loss of data, the position of the "write pointer" overrides the position of the "read pointer". After any append, the write pointer bounces to the new EOF
.
The answer at this link references the appropriate section of the C standard.
Use the "w+"
mode if you would like to write to arbitrary places in file. An existing file will be overwritten.
If you would like to append to an existing file initially, but then fseek
to arbitrary place, use "r+"
followed by fseek(f, 0, SEEK_END)
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…