解析c中stdout与stderr容易忽视的一些细节
更新时间:2020年4月25日 17:46 点击:1954
先看下面一个例子
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(), 或者采用下面的系统调用:
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."
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(), 或者采用下面的系统调用:
复制代码 代码如下:
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."
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
详解C语言中的rename()函数和remove()函数的使用方法
这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25- 这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
- 今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
- 很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
- 这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25
- 下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言之整数划分问题(递归法)实例代码的相关资料,需要的朋友可以参考下...2020-04-25
- 本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。...2020-04-25
C语言正则表达式详解 regcomp() regexec() regfree()用法详解
C语言处理正则表达式常用的函数有regcomp()、regexec()、regfree()和regerror(),这里就为大家介绍一下,需要的朋友可以参考一下啊...2020-04-25- 这篇文章主要介绍了c语言实现找最大值最小值位置查找,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-04