Linux中的stdio有著一定的行為準則,但常摸不頭緒。在Google大神中找到了一篇文章,解釋得很棒,值得參考參考。一個普遍遇到的問題就是在寫C code時,printf()有時會馬上輸出到stdout,但遇到了換行字元 "\n" 又會馬上寫出,自行呼叫 fflush(stdout) 又可以強迫將buffered的資料全寫出。
其實結論大概可以總結如下:
Default Buffering modes:
- stdin is always buffered
- stderr is always unbuffered
- if stdout is a terminal then buffering is automatically set to line buffered, else it is set to buffered
Default Buffer sizes:
- Buffer size only directly affects buffered mode
- The default size like the kernel is based on the page size (4096 bytes on my system)
- if stdin/stdout are connected to a terminal then default size = 1024; else size = 4096
.buffering in standard streams:
http://www.pixelbeat.org/programming/stdio_buffering/
[+/-] Read More (詳全文)...
[+/-] Summary only (僅顯示摘要)...