while (1) { printf("Any text"); sleep(1); }
但如果我添加换行符,它会.
经过几次实验,我发现在我的机器上stdout缓冲区被刷新:
>当我输入1025个字符或更多字符时;
>当我读到标准时;
>当我将换行符添加到stdout时;
第一个条件是完全清楚的 – 当缓冲区已满时,应该刷新它.第二个也是合理的.但为什么换行符导致潮红?其他隐含条件是什么?
解决方法
When a stream is unbuffered,characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block.
When a stream is fully buffered,characters are intended to be transmitted to or from the host environment as a block when a buffer is filled.
When a stream is line buffered,characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore,characters are intended to be transmitted as a block to the host environment when a buffer is filled,when input is requested on an unbuffered stream,or when input is requested on a line buffered stream that requires the transmission of characters from the host environment.
Support for these characteristics is implementation-defined,… C11dr §7.21.3 3
I’m just curIoUs which conditions should be satisfied to flush stdout buffer automatically.
原文地址:https://www.jb51.cc/c/119924.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。