Q: I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working.
A: Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is always required between reading and writing in the read/write "+" modes.
My question is why fseek
or fflush
is always required between reading and writing in the read/write "+" modes? Section 5.2 of c traps and pitfall
mentioned that it is because of backward compatibility issue. Any one can explain in details? Thanks.