什么是 C 中的更新流?

What is an update stream in C?

在 n1256 7.19.5.2 第 2 段(加粗):

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.

如果没有'update stream'这个词,整段话都说得通了。但我不知道那是什么。标准本身并没有介绍它。 Google 搜索似乎不起作用。什么意思?

术语“更新流”仅表示可读和可写的流。

这在 §7.19.5.3 第 3 点中指定(link,粗体):

#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).

在此上下文中,术语“更新”是指为读写而打开的文件。

该术语在 C99 standard 的第 7.19.5.3 节中的 fopen 函数规范中使用:

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