为什么不printf的呼叫后,除非换行符是格式字符串冲洗? [英] Why does printf not flush after the call unless a newline is in the format string?

查看:111
本文介绍了为什么不printf的呼叫后,除非换行符是格式字符串冲洗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么的printf 不冲水通话,除非换行符是格式字符串后?这是POSIX的行为呢?我怎么可能有的printf 立即冲洗每次?

Why does printf not flush after the call unless a newline is in the format string? Is this POSIX behavior? How might I have printf immediately flush every time?

推荐答案

标准输出流缓冲,所以将只显示什么是在缓冲区它达到一个换行符后(或者当它告知)。你有几个选项立即打印:

The stdout stream is buffered, so will only display what's in the buffer after it reaches a newline (or when it's told to). You have a few options to print immediately:

打印到标准错误,而不是使用 fprintf中

Print to stderr instead using fprintf:

fprintf(stderr, "I will be printed immediately");

当你需要它使用 fflush 刷新标准输出:

printf("Buffered, will be flushed");
fflush(stdout); // Will now print everything in the stdout buffer

修改:从下面安迪·罗斯的评论,你也可以在标准输出禁用缓冲使用则setbuf

Edit: From Andy Ross's comment below, you can also disable buffering on stdout by using setbuf:

setbuf(stdout, NULL);

这篇关于为什么不printf的呼叫后,除非换行符是格式字符串冲洗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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