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

3

16

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 Andrew Koenig's C Traps and Pitfalls (1989) mentioned that it is because of a backward compatibility issue. Can anyone explain in detail?

Callahan answered 11/11, 2009 at 8:29 Comment(3)
"c traps and pitfalls" is over 20 years old - do you really need to be backward compatible with stuff that was old 20 years ago?Anglin
Even if the book is 20 years old, those rules still stand.Roughshod
Note: The relevant section of C Standard for this requirement is C11 7.21.5.3/7Akeyla
N
17

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.

Nones answered 11/11, 2009 at 8:43 Comment(0)
M
5

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.

Mandle answered 11/11, 2009 at 8:44 Comment(0)
A
5

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.


Note that the standard mandates this. This is from the C11 standard, ISO/IEC 9899:2011, but the wording was similar in prior editions:

§7.21.5.3 The fopen function

¶7 When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.

Anarthria answered 11/11, 2009 at 8:47 Comment(3)
@JonathanLeffler If I intend to write to the first half of an existing file,and then read the remaining old data immediately after that,since I need to flush between read and write mode,do I need to use fseek(pFile,0,SEEK_CUR) to make sure buffer gets flushed due to fseek() set file pointer position remains unchanged?I had put it in a question but got no answers!!Saval
@JonathanLeffler Further it is said in the reference that flushing is needed before a write if reading operation which did not reach the end-of-file.Indirectly,does it mean to say if reading reaches end-of-file AND we want to write from that end,we don't require the buffer to be flushed?Saval
@Thokchom: Four years later — sorry for the delay: the standard says if you read and encounter EOF, then you can immediately write without a seek operation. See the quote in my answer.Anarthria

© 2022 - 2024 — McMap. All rights reserved.