欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

解析c中stdout與stderr容易忽視的一些細節(jié)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

先看下面一個例子
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
我們看到, 重定向到一個文件后, 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;
}

這樣重定向到一個文件后就正常了. 但是這種方法只適用于少量的輸出, 全局的設置方法還需要用 setbuf() 或 setvbuf(), 或者采用下面的系統(tǒng)調用:
復制代碼 代碼如下:

int main(int argc, char *argv[])
{
 write(1, "normal\n", strlen("normal\n"));
 write(2, "bad\n", strlen("bad\n"));
 return 0;
}

但是盡量不要同時使用 文件流 和 文件描述符,
復制代碼 代碼如下:

"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."

上一篇:深入Linux grep指令的詳解(實用型)

欄    目:C語言

下一篇:解析如何在C語言中調用shell命令的實現(xiàn)方法

本文標題:解析c中stdout與stderr容易忽視的一些細節(jié)

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4462.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有