linux下的stdin,stdout和stderr

参考自
在linux中经常会看到stdin,stdout和stderr,这3个可以称为终端(Terminal)的标准输入(standard input),标准输出(standard out)和标准错误输出(standard error)。

通过man stdin查看手册,可以看到它们都是在stdio.h中定义的。当linux开始执行程序的时候,程序默认会打开这3个文件流,这样就可以对终端进行输入输出操作。

下面用c语言模拟下这3个文件流。

标准输入(standard input)

在c语言中表现为调用scanf函数接受用户输入内容,即从终端设备输入内容。也可以用fscanf指明stdin接收内容。 标准输入的文件标识符为0

#include <stdio.h>
 
intmain(void)
{
    charstr[10];
    scanf("%s", str);
    fscanf(stdin, "%s", str);
 
    return0;
}

标准输出(standard out)

在c语言中表现为调用printf函数将内容输出到终端上。使用fprintf也可以把内容输出到终端上。标准输出的文件标识符为1

#include <stdio.h>
 
intmain(void)
{
    printf("%s\n", "hello");
    fprintf(stdout, "%s\n", "hello");
 
    return0;
}

标准错误输出(standard error)

标准错误和标准输出一样都是输出到终端上, 标准错误输出的文件标识符为2。

在程序处理的时候,正常的信息输出作为标准输出,产生错误的输出作为标准错误输出。这样在重定向的时候,就可以将正常的信息和错误的信息重定向到不同的文件。

#include <stdio.h>
 
intmain(void)
{
    printf("%s\n", "hello");
    fprintf(stderr, "%s\n", "error");
 
    return0;
}

上面这个程序分别往终端和stderr输出了信息,编译这个程序生成二进制文件a.out,使用下列命令运行,重定向输出。

# 1是stdout的文件标识符,而2是stderr的文件标识符
# "hello\n"重定向(输出)至log.txt,而"error\n"重定向至error.txt
./a.out 1>log.txt 2>error.txt

这样就把输出的内容hello保存到了log.txt文件,错误的信息error保存到了error.txt文件。(也就是通过管道重定位)

在c语言里,也可以使用freopen函数重定向输出流

#include <stdio.h>
 
intmain(void)
{
    FILE*out = freopen("out.txt", "w", stdout);
    printf("%s\n", "hello");
 
    return0;
}

上例代码编译执行后,终端上并没输出内容,而是保存到了out.txt这个文件了。

stdout与stderr的区别

1. 在linux中,标准输出和标准错误默认都是将信息输出到终端上,那么他们有什么区别呢?

让我们来看个题目:
问题:下面程序的输出是什么?

#include <stdio.h>

int main()
{
    fprintf(stdout,"Hello ");
    fprintf(stderr,"World!");

    return 0;
}

答案是:World!Hello

这是为什么呢?**在默认情况下,stdout是行缓冲的,它的输出会先放在一个buffer里面,只有到换行的时候,才会输出到屏幕;而stderr是无缓冲的,会直接输出。**举例来说就是fprintf(stdout, “xxxx”) 和 fprintf(stdout, “xxxx\n”),前者在返回时并不刷新buffer,直到遇到新行或函数返回时才会刷新缓冲区并输出;而fprintf(stderr, “xxxxx”),不管有没有’\n’,都直接输出。

首先,程序执行第一条打印语句时,由于stdout是行缓冲,而这里没有换行符,所以函数返回,要打印的内容还留在缓冲区里; 然后,执行第二条打印语句,由于stderr是无缓冲,World!直接打印在终端上; 最后,函数执行返回语句return,缓冲区中的Hello 此时被冲洗,在终端打印出来。

所以,整个程序的运行结果就是:World!Hello

2. 当stdout、stderr遇到重定向,会发生什么?

问题:下面的程序gcc编译之后分别执行下面两个命令,结果分别是什么?

./a.out 1>stdout.file 
./a.out 2>stderr.file
#include <stdio.h>

int main()
{
    printf("a\n");

    fprintf(stdout,"b\n");

    fprintf(stderr, "c\n");

    return 0;
}

结果:

# a, b输出至stdout.file,而屏幕显示c
./a.out 1>stdout.file 
# c输出至stderr.file,而屏幕显示a, b
./a.out 2>stderr.file

总结

总的来说,stdin,stdout和stderr还是和终端有密切关系,通常在生产环境时,会将这3个流重定向到其它文件。

猜你喜欢

转载自blog.csdn.net/jason_cuijiahui/article/details/84632616