setvbuf的size参数对于未缓冲的流意味着什么? [英] What does the size argument of setvbuf mean for an unbuffered stream?
问题描述
函数setvbuf()
可用于使流无缓冲:
The function setvbuf()
can be used to make a stream unbuffered:
#include <stdio.h>
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
-
当
mode
作为_IONBF
传递时,size
参数的值是什么意思?What does the value of the
size
argument mean when themode
is passed as_IONBF
?是否将分配缓冲区?
可以通过
0
吗?推荐答案
C标准的当前版本(C11)显示:
The current version (C11) of the C Standard says:
7.21.5.6
setvbuf
功能7.21.5.6 The
setvbuf
function简介
#include <stdio.h> int setvbuf(FILE * restrict stream, char * restrict buf, int mode, size_t size);
说明
仅在由
stream
指向的流已经与打开的文件相关联之后并且对该流执行任何其他操作(对setvbuf
的调用失败除外)之前,可以使用setvbuf
函数.参数mode
确定如何对流进行缓冲,如下所示:_IOFBF
使输入/输出得到完全缓冲;_IOLBF
使输入/输出被行缓冲;_IONBF
导致输入/输出未缓冲.如果buf
不是空指针,则可以使用它指向的数组代替setvbuf
函数 273 分配的缓冲区),并且参数size
指定数组的大小;否则,size
可以确定由setvbuf
函数分配的缓冲区的大小.数组中的内容随时都是不确定的.The
setvbuf
function may be used only after the stream pointed to bystream
has been associated with an open file and before any other operation (other than an unsuccessful call tosetvbuf
) is performed on the stream. The argumentmode
determines how stream will be buffered, as follows:_IOFBF
causes input/output to be fully buffered;_IOLBF
causes input/output to be line buffered;_IONBF
causes input/output to be unbuffered. Ifbuf
is not a null pointer, the array it points to may be used instead of a buffer allocated by thesetvbuf
function273) and the argumentsize
specifies the size of the array; otherwise,size
may determine the size of a buffer allocated by thesetvbuf
function. The contents of the array at any time are indeterminate.返回
setvbuf
函数成功返回零,如果mode
的值无效或不能满足请求,则返回非零.The
setvbuf
function returns zero on success, or nonzero if an invalid value is given formode
or if the request cannot be honored.条件时态的使用为库作者提供了很多实现
setvbuf()
的灵活性.The use of the conditional tense gives a lot of flexibility for the library author as to how to implement
setvbuf()
.史蒂夫·萨米特(Steve Summit)这样争论:
Steve Summit arguments this way:
size
,并且显然0
是要传递的适当值. (也就是说,如果您传递的不是0的任何数字,它仍然会被忽略,并且将不会分配任何内容,但是该调用看起来会令人产生严重的误导.)但是,我刚刚看过的两个不同的手册页并没有出现并明确地说出来.Clearly
size
should be ignored ifmode
is passed as_IONBF
, and clearly0
is the appropriate value to pass. (That is, if you passed any number other than 0, it would still be ignored, and nothing would be allocated, but the call would look horribly misleading.) But two different man pages I've just looked at don't come out and say this explicitly.C标准确实允许在所有情况下都忽略
size
,并且在无缓冲情况下也可以忽略size
.The C Standard does indeed allow for
size
to be ignored for all cases and it does make sense that it be ignored for the unbuffered case.我发现了一个有意义的替代方法:
setvbuf()
仍然可以指定流输出函数使用的缓冲区,无论是静态的还是分配的,只要流函数返回时不保留任何未刷新的输出即可.例如,printf
可以使用此缓冲区来组合其输出并按块将其刷新. C标准没有未指定未缓冲的流对写入输出流的系统句柄的每个字节使用系统调用.这些是超出标准范围的实施细节.I found an alternative that makes sense too:
setvbuf()
could still specify the buffer to be used by the stream output functions, static or allocated, as long as the stream functions do not keep any unflushed output in it when they return. For example,printf
could use this buffer to compose its output and flush it by chunks. The C Standard does not specify that unbuffered streams use a system call for each byte written to the output stream's system handle. These are implementation details beyond the scope of the Standard.如果
setvbuf()
确实试图分配size
字节并失败,则它可能返回非零值,并且不会使流不被缓冲.仍然符合要求的意外行为.尝试setvbuf(stdout, NULL, _IOFBF, SIZE_MAX);
If
setvbuf()
indeed tries to allocatesize
bytes and fails, it may return a non zero value and not make the stream unbuffered. An unexpected behavior that would still be conformant. Trysetvbuf(stdout, NULL, _IOFBF, SIZE_MAX);
这篇关于setvbuf的size参数对于未缓冲的流意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!