最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年05月27日 15:59:11   作者:  
本篇文章是對在c語言中stdout與stderr容易忽視的一些細節(jié)進行了詳細的分析介紹,需要的朋友參考下
先看下面一個例子
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."

相關文章

最新評論

普定县| 融水| 甘南县| 石阡县| 鄂伦春自治旗| 江油市| 黎平县| 大港区| 化德县| 玉田县| 镇康县| 白沙| 靖远县| 海阳市| 湘乡市| 林州市| 衢州市| 庄河市| 雅江县| 桐柏县| 海门市| 新宾| 潼南县| 涟源市| 句容市| 三亚市| 平阳县| 武穴市| 清徐县| 呼伦贝尔市| 永宁县| 漳州市| 留坝县| 新巴尔虎右旗| 灵石县| 永嘉县| 广宁县| 乐山市| 怀安县| 莱西市| 都匀市|