Why is fseek or fflush always required between reading and writing in the update modes?

The library buffers input and output operations. Check out setvbuf() and the _IOFBF, _IOLBF parameters to that function.

fseek() or fflush() require the library to commit buffered operations.

The standard specifies a seek or flush operation (flushing the buffers) as mandatory prior to changing I/O direction to allow the library some shortcuts. Without this restriction, the library would have to check for every I/O operation if the previous operation was the same direction (reading / writing), and trigger a flush by itself if the I/O direction changed. With the restriction as-is, the library may assume the client did the seek / flush before changing I/O direction, and can omit the direction checks.

Leave a Comment