五、stdout,stdoin和stderr

stdout,stdin和stderr分别是标准输出流标准输入流标准错误流,当一个用户进程被创建的时候,系统会自动为该进程创建这三个数据流,默认情况下这三个流是在终端上表现出来的。可以使用fprintf函数将数据打印到流中,如调用函数fprintf(stdout, "hello world\n");或者fprintf(stderr, "hello world\n");则会在终端中显示“hello world”,调用fgets(buf, 32, stdin);则表示从终端获取一行数据。

stdout和stderr的区别就是stdout是有缓存的,而stderr是没有缓存的,最直观的体现就是输出到stdout和stderr中的字符什么时候在终端中显示。对于stderr而言,输出到stderr中的字符立马会在终端中显示出来,而对于stdout而言要满足一下几个条件才会在屏幕中显示出来:

1)遇到'\n'

2)流关闭

3)用fflush函数刷新

4)缓冲区即将溢出

下面对以上情况一一举例说明,编写下列代码:

1 #include <stdio.h>
2 
3 int main(int argc, const char *argv[])
4 {
5     fprintf(stdout, "hello world");
6     while(1);
7 
8     return 0;
9 }

该程序执行后终端是没有打印的,因为不满足上述4种情况中的任何一种。第6行的死循环是为了避免流关闭。

将代码修改如下:

1 #include <stdio.h>
2 
3 int main(int argc, const char *argv[])
4 {
5     fprintf(stdout, "hello\nworld");
6     while(1);
7 
8     return 0;
9 }

在“hello”和"world"之间插入一个'\n',可以看到终端中输出了“hello”但是没有输出“world”,验证了第1种情况。

将代码修改如下:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     fprintf(stdout, "hello world");
 6     fclose(stdout);
 7     while(1);
 8 
 9     return 0;
10 }

将标准输出流关闭,可以看到输出的“hello world”,验证了第2种情况。

将代码修改如下:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     fprintf(stdout, "hello world");
 6     fflush(stdout);
 7     while(1);
 8 
 9     return 0;
10 }

刷新输出流缓冲区,可以看到输出的“hello world”,验证了第3种情况。第4种情况与流的缓存类型有关,缓存类型分为无缓冲、行缓冲和全缓冲,行缓冲的缓冲区大小为1024个字节,全缓存的缓冲区大小为4096个字节,默认情况下与文件建立的缓冲类型为全缓冲,与终端建立的缓冲类型为行缓冲。

将代码修改如下:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     int i;
 6 
 7     for (i = 0; i < 1025; i++) {
 8         fprintf(stdout, "a");
 9     }
10 
11     while(1);
12 
13     return 0;
14 }

终端将显示1024个‘a’(当然我没数),而将第7行的1025改为1024则终端不会显示。

 

可以使用setvbuf函数改变流的缓冲类型。

原型:int setvbuf(FILE *stream, char *buf, int mode, size_t size);

参数:

  stream:流指针

  buf:分配给用户的缓冲。如果设置为 NULL,该函数会自动分配一个指定大小的缓冲

  mode:缓冲类型

    _IONBUF:无缓冲

    _IOLBUF:行缓冲

    _IOFBUF:全缓冲

  size:缓冲区大小

将代码修改如下:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     setvbuf(stdout, NULL, _IONBF, 1024);
 6     fprintf(stdout, "a");
 7 
 8     while(1);
 9 
10     return 0;
11 }

运行程序,在屏幕上是会显示出字符‘a'的,因为使用setvbuf将标准输出流设置成了无缓冲。

在测试时发现以下几个现象,注意标红的地方:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     int i;
 6 
 7     setvbuf(stdout, NULL, _IOLBF, 5);
 8     for (i = 0; i < 1025; i++) {
 9         fprintf(stdout, "a");
10     }
11 
12     while(1);
13 
14     return 0;
15 }

运行该程序有输出,而将1025改为1024则没有输出,将_IOLBF改为_IOFBF也是1024没有输出,1025有输出。

修改代码如下:

 1 #include <stdio.h>
 2 
 3 int main(int argc, const char *argv[])
 4 {
 5     int i;
 6     char buf[4097];
 7 
 8     setvbuf(stdout, buf, _IOLBF, 5);
 9     for (i = 0; i < 5; i++) {
10         fprintf(stdout, "a");
11     }
12 
13     while(1);
14 
15     return 0;
16 }

运行程序,屏幕不显示,将第9行标红的5改为6则屏幕有显示,将第8行的_IOLBF改为_IOFBF现象也是一样的。

这让我产生一种感觉:

  1)setvbuf第二个参数为NULL时,第4个参数是不起作用的。

  2)默认stdout的行缓冲和全缓冲大小都是1024个字节。

但是我记得默认的全缓冲大小为4096个字节呀。。。

 

转载于:https://www.cnblogs.com/Suzkfly/p/10367376.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值