一、stderr与stdout的区别

来源:互联网 发布:力宇刻字机端口设置 编辑:程序博客网 时间:2024/05/16 15:38
  • 介绍一下:stdout, stdin, stderr的区别;

stdout, stdin, stderr的中文名字分别是标准输出,标准输入和标准错误。

当一个用户进程被创建的时候,系统会自动为该进程创建三个数据流, 一个程序要运行,需要有输入、输出,如果出错,还要能表现出自身的错误。这是就要从某个地方读入数据、将数据输出到某个地方,这就够成了数据流。

因此,一个进程初期所拥有的这么三个数据流,就分别是标准输出、标准输入和标准错误,分别用stdout, stdin, stderr来表示。。这3个文件分别为标准输入(stdin)、标准输出(stdout)、标准错误(stderr)。它们在

#include<iostream>#include<cstdio>using namespace std;int main(){fprintf(stdout,"Hello\n");fprintf(stderr,"World!");return 0;}

Hello
world!

#include<iostream>#include<cstdio>using namespace std;int main(){fprintf(stdout,"Hello ");fprintf(stderr,"World!");return 0;}

World!Hello

在默认情况下,stdout是行缓冲的,他的输出会放在一个buffer里面,只有到换行的时候,才会输出到屏幕。而stderr是无缓冲的,会直接输出,举例来说就是fprintf(stdout, “xxxx”) 和 fprintf(stdout,”xxxx\n”),前者会缓存,直到遇到新行才会一起输出。而fprintf(stderr, “xxxxx”),不管有么有\n,都输出。

简单一句话:stderr与stdout的区别

stdout(标准输出),输出方式是行缓冲。输出的字符会先存放在缓冲区,等按下回车键时才进行实际的I/O操作。

stderr(标准出错),是不带缓冲的,这使得出错信息可以直接尽快地显示出来。

原创粉丝点击