What is an update stream in C?

Viewed 104

At n1256 7.19.5.2 paragraph 2 (with my bold):

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

If there was not the word 'update stream', the whole paragraph would make sense. But I don't know what it is. Standard itself doesn't introduce it. Google search doesn't seem to work. What's the meaning of it?

2 Answers

The term "update stream" simply means a stream that is both readable and writable.

This is specified in §7.19.5.3 point 3 (link, bold mine):

#include <stdio.h>
FILE *fopen(const char * restrict filename,const char * restrict mode);

[...]

The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behavior is undefined(237).

  • r: open text file for reading
  • w: truncate to zero length or create text file for writing
  • a: append; open or create text file for writing at end-of-file
  • rb: open binary file for reading
  • wb: truncate to zero length or create binary file for writing
  • ab: append; open or create binary file for writing at end-of-file
  • r+: open text file for update (reading and writing)
  • w+: truncate to zero length or create text file for update
  • a+: append; open or create text file for update, writing at end-of-file(237)
  • r+b or rb+: open binary file for update (reading and writing)
  • w+b or wb+: truncate to zero length or create binary file for update
  • a+b or ab+: append; open or create binary file for update, writing at end-of-file

(237) If the string begins with one of the above sequences, the implementation might choose to ignore the remaining characters, or it might use them to select different kinds of a file (some of which might not conform to the properties in 7.19.2).

The term "update" in this context means a file opened for both reading and writing.

The term is used in the specification of the fopen function in section 7.19.5.3 of the C99 standard:

3 The argument mode points to a string.If the string is one of the following, the file is open in the indicated mode.Otherwise, the behavior is undefined.

r open text file for reading

w truncate to zero length or create text file for writing

a append; open or create text file for writing at end-of-file

rb open binary file for reading

wb truncate to zero length or create binary file for writing

ab append; open or create binary file for writing at end-of-file

r+ open text file for update (reading and writing)

w+ truncate to zero length or create text file for update

a+ append; open or create text file for update, writing at end-of-file

r+b or rb+ open binary file for update (reading and writing)

w+b or wb+ truncate to zero length or create binary file for update

a+b or ab+ append; open or create binary file for update, writing at end-of-file

...

6 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

Related