setvbuf的size参数对于未缓冲的流意味着什么? [英] What does the size argument of setvbuf mean for an unbuffered stream?

查看:129
本文介绍了setvbuf的size参数对于未缓冲的流意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数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 the mode 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 by stream has been associated with an open file and before any other operation (other than an unsuccessful call to setvbuf) is performed on the stream. The argument mode 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. If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by the setvbuf function273) and the argument size specifies the size of the array; otherwise, size may determine the size of a buffer allocated by the setvbuf 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 for mode 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 if mode is passed as _IONBF, and clearly 0 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 allocate size bytes and fails, it may return a non zero value and not make the stream unbuffered. An unexpected behavior that would still be conformant. Try setvbuf(stdout, NULL, _IOFBF, SIZE_MAX);

      这篇关于setvbuf的size参数对于未缓冲的流意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆