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

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

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

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

先看下面一個(gè)例子
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."

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

欄    目:C語(yǔ)言

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

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

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

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

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

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有