...
When a file is opened with update mode 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
, orrewind
), 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.
The following scenarios will be diagnosed because either can result in undefined behavior:
⢠Receiving input from a stream directly following an output to that stream without an intervening call to fflush()
, fseek()
, fsetpos()
, or rewind()
, or outputting if the file is not at end-of-file
⢠Outputting to a stream after receiving input from it that stream without a call to fseek()
, fsetpos()
, or rewind()
if the file is not at end-of-file
results in undefined behavior. (See also undefined behavior 143 in Annex J of C99.) Consequently, a call to fseek()
, fflush()
or fsetpos()
is necessary between input and output to the same stream. (See recommendation FIO07-C. Prefer fseek() to rewind().)
...