linux下程序的标准输入流输出流错误流

来源:互联网 发布:网络程蝶依视频 编辑:程序博客网 时间:2024/06/06 02:41

首先是代码

#include <stdio.h>


int main()
{
printf("hello world!\n");//此行代码就是由我们stdio.h中的标准输出流完成的
int a;
scanf("%d",&a);//此行代码就是由我们stdio.h中的标准输入流完成的
printf("input value is :%d\n",a);
return 0;
}

当我生成可执行文件时,会生成3个标准文件

stdin

stdout

stderr

对我们的代码进行改造

#include <stdio.h>


int main()
{
/*
printf("hello world!\n");//此行代码就是由我们stdio.h中的标准输出流完成的
int a;
scanf("%d",&a);//此行代码就是由我们stdio.h中的标准输入流完成的
printf("input value is :%d\n",a);
return 0;
*/


//printf("please input the value :\n");
fprintf(stdout,"please input the value :\n");
//fprint第一个参数就是一个资源句柄,printf其实就是一个对fprintf的封装。



int a;
//scanf("%d",&a);
fscanf(stdin,"%d",&a);

if(a<0){
fprintf(stderr,"the value must >0");
return 1;
}

}


当我们输入1的时候输出1

当我们输入-2的时候则走入到错误流。

事实上每一个c程序跑起来都是要生成一个进程。 在该进程中都会生成3个文件句柄。

stdin

stdout

stderr

这个很重要。

阅读全文
0 0
原创粉丝点击