解析c中stdout與stderr容易忽視的一些細(xì)節(jié)
更新時(shí)間:2013年05月27日 15:59:11 作者:
本篇文章是對(duì)在c語(yǔ)言中stdout與stderr容易忽視的一些細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
先看下面一個(gè)例子
a.c :
int main(int argc, char *argv[])
{
fprintf(stdout, "normal\n");
fprintf(stderr, "bad\n");
return 0;
}
$ ./a
normal
bad
$ ./a > tmp 2>&1
$ cat tmp
bad
tmp
我們看到, 重定向到一個(gè)文件后, bad 到了 normal 的前面.
原因如下:
"The stream stderr is unbuffered. The stream stdout is line-buffered when it points to a
terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline
is printed. This can produce unexpected results, especially with debugging output. The
buffering mode of the standard streams (or any other stream) can be changed using the
setbuf(3) or setvbuf(3) call. "
因此, 可以使用如下的代碼:
int main(int argc, char *argv[])
{
fprintf(stdout, " normal\n");
fflush(stdout);
fprintf(stderr, " bad\n");
return 0;
}
這樣重定向到一個(gè)文件后就正常了. 但是這種方法只適用于少量的輸出, 全局的設(shè)置方法還需要用 setbuf() 或 setvbuf(), 或者采用下面的系統(tǒng)調(diào)用:
int main(int argc, char *argv[])
{
write(1, "normal\n", strlen("normal\n"));
write(2, "bad\n", strlen("bad\n"));
return 0;
}
但是盡量不要同時(shí)使用 文件流 和 文件描述符,
"Note that mixing use of FILEs and raw file descriptors can produce unexpected results and
should generally be avoided. A general rule is that file
descriptors are handled in the kernel, while stdio is just a library. This means for exam-
ple, that after an exec(), the child inherits all open file descriptors, but all old
streams have become inaccessible."
a.c :
復(fù)制代碼 代碼如下:
int main(int argc, char *argv[])
{
fprintf(stdout, "normal\n");
fprintf(stderr, "bad\n");
return 0;
}
$ ./a
normal
bad
$ ./a > tmp 2>&1
$ cat tmp
bad
tmp
我們看到, 重定向到一個(gè)文件后, bad 到了 normal 的前面.
原因如下:
復(fù)制代碼 代碼如下:
"The stream stderr is unbuffered. The stream stdout is line-buffered when it points to a
terminal. Partial lines will not appear until fflush(3) or exit(3) is called, or a newline
is printed. This can produce unexpected results, especially with debugging output. The
buffering mode of the standard streams (or any other stream) can be changed using the
setbuf(3) or setvbuf(3) call. "
因此, 可以使用如下的代碼:
復(fù)制代碼 代碼如下:
int main(int argc, char *argv[])
{
fprintf(stdout, " normal\n");
fflush(stdout);
fprintf(stderr, " bad\n");
return 0;
}
這樣重定向到一個(gè)文件后就正常了. 但是這種方法只適用于少量的輸出, 全局的設(shè)置方法還需要用 setbuf() 或 setvbuf(), 或者采用下面的系統(tǒng)調(diào)用:
復(fù)制代碼 代碼如下:
int main(int argc, char *argv[])
{
write(1, "normal\n", strlen("normal\n"));
write(2, "bad\n", strlen("bad\n"));
return 0;
}
但是盡量不要同時(shí)使用 文件流 和 文件描述符,
復(fù)制代碼 代碼如下:
"Note that mixing use of FILEs and raw file descriptors can produce unexpected results and
should generally be avoided. A general rule is that file
descriptors are handled in the kernel, while stdio is just a library. This means for exam-
ple, that after an exec(), the child inherits all open file descriptors, but all old
streams have become inaccessible."
相關(guān)文章
圖解AVL樹(shù)數(shù)據(jù)結(jié)構(gòu)輸入與輸出及實(shí)現(xiàn)示例
這篇文章主要為大家介紹了C++圖解AVL樹(shù)數(shù)據(jù)結(jié)構(gòu)輸入與輸出操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05C++圖論之Bellman-Ford算法和SPFA算法的實(shí)現(xiàn)
貝爾曼-福特算法(Bellman-Ford)是由理查德·貝爾曼和萊斯特·福特創(chuàng)立的,求解單源最短路徑問(wèn)題的一種算法。SPFA 算法是 Bellman-Ford算法 的隊(duì)列優(yōu)化算法的別稱(chēng),通常用于求含負(fù)權(quán)邊的單源最短路徑。本文將詳解兩個(gè)算法的實(shí)現(xiàn),需要的可以參考一下2022-06-06關(guān)于UDP服務(wù)器客戶(hù)端編程流程介紹
大家好,本篇文章主要講的是關(guān)于UDP服務(wù)器客戶(hù)端編程流程介紹,感興趣的同學(xué)趕快來(lái)看看吧,對(duì)你有幫助的話記得收藏2021-12-12基于C語(yǔ)言編寫(xiě)一個(gè)簡(jiǎn)單的Web服務(wù)器
C語(yǔ)言可以干大事,這篇文章主要為大家詳細(xì)介紹了如何基于C語(yǔ)言可以完成一個(gè)簡(jiǎn)易的Web服務(wù)器,希望這篇文章會(huì)幫你你對(duì)C語(yǔ)言有更深入的理解2024-03-03Linux網(wǎng)絡(luò)編程之socket文件傳輸示例
這篇文章主要介紹了Linux網(wǎng)絡(luò)編程之socket文件傳輸示例,對(duì)于基于Linux平臺(tái)的C程序員來(lái)說(shuō)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08