C语言 如何在 C 中关闭标准输出的缓冲
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7876660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to turn off buffering of stdout in C
提问by Sreenath Nannat
I want to turn off the buffering for the stdout for getting the exact result for the following code
我想关闭标准输出的缓冲以获得以下代码的确切结果
while(1) {
printf(".");
sleep(1);
}
The code printf bunch of '.' only when buffer gets filled.
代码 printf 一堆 '.' 只有当缓冲区被填满时。
采纳答案by tdenniston
Use fflush(stdout). You can use it after every printfcall to force the buffer to flush.
使用fflush(stdout). 您可以在每次printf调用后使用它来强制刷新缓冲区。
回答by Frerich Raabe
You can use the setvbuf function:
您可以使用setvbuf 函数:
setvbuf(stdout, NULL, _IONBF, 0);
The link above has been broken. Here're another links to the function.
上面的链接已经失效。这是该函数的另一个链接。
回答by Eswar Yaganti
You can also use setbuf
您也可以使用 setbuf
setbuf(stdout, NULL);
This will take care of everything
这将处理一切
回答by NickLH
Use fflush(FILE *stream)with stdoutas the parameter.
使用fflush(FILE *stream)withstdout作为参数。
回答by Mickael Ciocca
You can do this:
你可以这样做:
write(1, ".", 1);
instead of this:
而不是这个:
printf(".");

