tags:

views:

263

answers:

3

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.

+5  A: 

The library buffers input and output operations. Check out setvbuf() and the _IOFBF, _IOLBF parameters to that funktion. fseek() or fflush() require the library to commit buffered operations. The standard specifies a seek or flush operation as mandatory to allow the library some shortcuts; otherwise, for every I/O operation, the lib would have to check if the previous operation was also a read op (or a write op), and trigger a flush by itself if the "direction" of the I/O changed.

DevSolar
+2  A: 

Because it keeps OS/library code simpler. A file stream may have separate read and write buffers, and extra effort would be required to make sure they are always synchronised. This would cost performance at times when it wasn't needed.

So instead, the programmer needs to do this explicitly when it is needed.

Artelius
+3  A: 

Read Plauger's "The Standard C Library" for some insights into why various features of the (C89) standard library are as they are - and in particular why parts of the standard I/O library are as they are. One reason is that C runs on very diverse systems and with diverse media; devices such as tapes may well need to be handled somewhat differently from the disk drive you're accustomed to thinking of. Also, on Unix, consider your 'tty' device - it connects a keyboard and a mouse to a screen - three quite different bits of hardware. Coordinating between those is tricky enough; the rules in the standard make it easier.

Jonathan Leffler
Yep, excellent book.
DevSolar
+1 for the book.
pierr