简体   繁体   English

"如何在 C 中关闭标准输出的缓冲"

[英]How to turn off buffering of stdout in C

I want to turn off the buffering for the stdout for getting the exact result for the following code我想关闭标准输出的缓冲以获得以下代码的确切结果

while(1) {
printf(".");
sleep(1);
}

You can use the setvbuf function<\/a> :您可以使用setvbuf 功能<\/a>:

setvbuf(stdout, NULL, _IONBF, 0);

You can also use setbuf你也可以使用 setbuf

setbuf(stdout, NULL);

This will take care of everything这将照顾一切

Use fflush(FILE *stream) with stdout as the parameter.使用fflush(FILE *stream)stdout作为参数。

http://www.elook.org/programming/c/fflush.html http://www.elook.org/programming/c/fflush.html

You can do this:你可以这样做:

write(1, ".", 1);

Use fflush(stdout)<\/code> .使用fflush(stdout)<\/code> 。 You can use it after every printf<\/code> call to force the buffer to flush.您可以在每次printf<\/code>调用后使用它来强制刷新缓冲区。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM