默认情况下,stdout行是缓冲的,无缓冲的还是不确定的? [英] Is stdout line buffered, unbuffered or indeterminate by default?

查看:174
本文介绍了默认情况下,stdout行是缓冲的,无缓冲的还是不确定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c99 中的 7.9.13 / 7 部分指出:


在程序启动时,预定义了三个文本流,无需显式打开-标准输入(用于读取常规输入),标准输出(用于写入常规输出)和标准错误(用于写入)诊断输出)。


最初打开时,标准错误流未完全缓冲;当且仅当可以确定该流不引用交互式设备时,标准输入流和标准输出流才被完全缓冲。


所以这很有意义。如果要将标准输出推送到文件,则希望将其完全缓冲以提高效率。


但是我在标准中没有提到输出是行缓冲还是未缓冲当您不能确定设备是非交互式的(即终端的正常输出)时。


我问的原因是对我的回答的评论此处我应该在两个语句之间插入 fflush(stdout);

  printf( ;输入数字>")); 
// fflush(stdout);需要吗?
if(fgets(buff,sizeof(buff),stdin)== NULL){...}

因为我没有用换行符终止 printf 。谁能解决这个问题?

解决方案

C99标准未指定三个标准流是未缓冲还是行缓冲:这取决于实现。我知道的所有UNIX实现都有一行缓冲的 stdin 。在Linux上, stdout 行缓冲,而 stderr 无缓冲。



<据我所知,POSIX并未施加其他限制。 POSIX的 fflush 页在示例部分中进行了说明:


[...]使用 fflush()函数是因为通常缓冲标准输出,并且提示可能不会立即打印在输出或终端上。


因此,请注意添加 fflush(stdout); 是正确的。






另一种方法是制作 stdout 无缓冲:

  setbuf(stdout,NULL); 
/ *或* /
setvbuf(stdout,NULL,_IONBF,0);

但是如R.所述,您只能执行一次,并且必须在写入 stdout 或对其执行任何其他操作。 (C99 7.19.5.5 2)






我刚刚读了最近的线程 comp.lang.c 上一样。备注之一:


Unix约定是 stdin 和<$ c $与终端关联时,c> stdout 是行缓冲的,否则是全缓冲的(也就是块缓冲的)。 stderr 始终没有缓冲。



Section 7.9.13/7 of c99 states that:

At program start-up, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output).

As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

So that makes sense. If you're pushing your standard output to a file, you want it fully buffered for efficiency.

But I can find no mention in the standard as to whether the output is line buffered or unbuffered when you can't determine the device is non-interactive (ie, normal output to a terminal).

The reason I ask was a comment to my answer here that I should insert an fflush(stdout); between the two statements:

printf ("Enter number> ");
// fflush (stdout); needed ?
if (fgets (buff, sizeof(buff), stdin) == NULL) { ... }

because I wasn't terminating the printf with a newline. Can anyone clear this up?

解决方案

The C99 standard does not specify if the three standard streams are unbuffered or line buffered: It is up to the implementation. All UNIX implementations I know have a line buffered stdin. On Linux, stdout in line buffered and stderr unbuffered.

As far as I know, POSIX does not impose additional restrictions. POSIX's fflush page does note in the EXAMPLES section:

[...] The fflush() function is used because standard output is usually buffered and the prompt may not immediately be printed on the output or terminal.

So the remark that you add fflush(stdout); is correct.


An alternative could be to make stdout unbuffered:

setbuf(stdout, NULL);
/* or */
setvbuf(stdout, NULL, _IONBF, 0);

But as R. notes you can only do this once, and it must be before you write to stdout or perform any other operantion on it. (C99 7.19.5.5 2)


I just read a recent thread on comp.lang.c about the same thing. One of the remarks:

Unix convention is that stdin and stdout are line-buffered when associated with a terminal, and fully-buffered (aka block-buffered) otherwise. stderr is always unbuffered.

这篇关于默认情况下,stdout行是缓冲的,无缓冲的还是不确定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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