C- 文件 input/output 缓冲区和 setvbuf()
C- File input/output buffers and setvbuf()
在C语言中,fopen()是否真的创建了两个缓冲区,一个用于输入,另一个用于输出?
我的 C 书是这么说的:
Normally, the first step in using standard I/O is to use f open ( ) to
open a file. (Recall, however, that the stdin, stdout, and stderr
files are opened automatically.) The fopen( ) function not only opens
a file but sets up a buffer (two buffers for read-write modes), and it
sets up a data structure containing data about the file and ..
如果用fopen()打开一个文件会创建两个buffer,像"a+"这样的写模式,即读和写
FILE * fp = fopen ("file.txt", "a +");
setvbuf (destination_file, NULL, _IOFBF, BUFFER_SIZE);
setvbuf()函数指的是什么缓冲区?
what buffer does the setvbuf ()
function refer to?
"Both" 个。不要求调用 fopen()
会创建 "two buffers for read-write modes"。大多数实现都使用单个缓冲区,因为这就是所需要的。
The C standard implicitly supports a single buffer. Per 7.21.5.3 The fopen
function, paragraph 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.
该段的要求允许使用单个缓冲区。
一个打开的文件只有一个缓冲区,无论它是为读、写还是两者打开的。
C standard 的第 7.21.5.3 节详细介绍了 fopen
函数,指出:
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
,
fsetpo
s, 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.
上面的段落指出在执行输入之前必须刷新输出缓冲区(通过定位函数显式或隐式),在输入后执行输出时也是如此。这是只有一个缓冲区的结果。
从逻辑的角度来看,这也是有道理的,因为它可以防止读取和写入文件内容的视图不一致。
在C语言中,fopen()是否真的创建了两个缓冲区,一个用于输入,另一个用于输出?
我的 C 书是这么说的:
Normally, the first step in using standard I/O is to use f open ( ) to open a file. (Recall, however, that the stdin, stdout, and stderr files are opened automatically.) The fopen( ) function not only opens a file but sets up a buffer (two buffers for read-write modes), and it sets up a data structure containing data about the file and ..
如果用fopen()打开一个文件会创建两个buffer,像"a+"这样的写模式,即读和写
FILE * fp = fopen ("file.txt", "a +");
setvbuf (destination_file, NULL, _IOFBF, BUFFER_SIZE);
setvbuf()函数指的是什么缓冲区?
what buffer does the
setvbuf ()
function refer to?
"Both" 个。不要求调用 fopen()
会创建 "two buffers for read-write modes"。大多数实现都使用单个缓冲区,因为这就是所需要的。
The C standard implicitly supports a single buffer. Per 7.21.5.3 The fopen
function, paragraph 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 thefflush
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.
该段的要求允许使用单个缓冲区。
一个打开的文件只有一个缓冲区,无论它是为读、写还是两者打开的。
C standard 的第 7.21.5.3 节详细介绍了 fopen
函数,指出:
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 thefflush
function or to a file positioning function (fseek
,fsetpo
s, 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.
上面的段落指出在执行输入之前必须刷新输出缓冲区(通过定位函数显式或隐式),在输入后执行输出时也是如此。这是只有一个缓冲区的结果。
从逻辑的角度来看,这也是有道理的,因为它可以防止读取和写入文件内容的视图不一致。