题
在Perl中,我可以输入:
$|++;
打印到STDOUT的任何内容都将自动fflush()编辑。
C中有等价物吗?换句话说,有什么方法可以告诉stdio在每次printf()之后自动刷新stdout,它会自动刷新stderr吗?
解决方案
尝试setvbuf(stdout, NULL, _IONBF, 0)
。它将stdout
更改为无缓冲(_IONBF
)模式。
其他提示
我没有这样做,但_IOLBF将是正确的答案。
$ man setvbuf
结果...
点击NAME
setvbuf - 为流分配缓冲
概要
#include <!> lt; stdio.h <!> gt;
int setvbuf(FILE *restrict stream, char *restrict buf, int type,
size_t size);
说明
setvbuf()函数可以在stream指向的流与打开的文件关联之后但在执行任何其他操作(除了对setvbuf()的不成功调用之外)之前使用。
流。参数类型确定如何缓冲流,如下所示:
* {_IOFBF} shall cause input/output to be fully buffered.
* {_IOLBF} shall cause input/output to be line buffered.
* {_IONBF} shall cause input/output to be unbuffered.
看看setbuf()和setvbuf()。
不隶属于 StackOverflow