#include?
#include?
int?main(void)
{??????????printf("hello?world");
close(STDOUT_FILENO);??????????return?0;
}//什么都不輸出12345678910111234567891011#include?
#include?
int?main(void)
{?????????printf("hello?world\n");
close(STDOUT_FILENO);?????????return?0;
}//hello?world12345678910111234567891011
通過上面的兩個程序已經證明在linux下,“\n”確實對緩沖區有強制刷新的作用#include?
#include?
#include?
#include?
#include?
#include?
int?main(void)
{??????????int?fd;
close(STDOUT_FILENO);
fd?=?open("hello",?O_CREAT|O_RDWR,?0644);??????????if(fd?
perror("open");??????????????????exit(0);
}??????????printf("Nice?to?meet?you!\n");??????????//fflush(stdout);
close(fd);??????????return?0;
}123456789101112131415161718192021123456789101112131415161718192021
但是第三段代碼,當沒有fflush函數強制刷新的時候,hello文件里面沒有“Nice to meet you!”,在加上fflush函數之后,hello文件中就有“Nice to meet you!”了。
但是我已經在”Nice to meet you!\n”后面加上換行符,不是應該刷新文件緩沖區,使得”Nice to meet you!\n”寫到hello文件當中去么?但是實際并沒有,這是為什么 ,求解惑有網友說是因為控制臺文件和常規文件的區別,那是不是可以這么認為,在C標準中,控制臺有單獨的緩存空間,有單獨的運行規則,比如遇到\n就執行刷新操作,而常規文件中的緩存空間是沒有這個設定的,有待進一步確定。
原因:只有stdout的緩沖區是通過‘\n’進行行刷新的,但是我開始的時候就把stdout就關閉了,就會像普通文件一樣像文件中寫,所以‘\n’是不會行刷新的,所以要使用fflush(stdout)。
stderr無緩沖,不用經過fflush或exit,就直接打印出來
stdout行緩沖 遇到\n刷新緩沖區
http://blog.csdn.net/a312024054/article/details/46946237