stdout,stdin浅析

来源:互联网 发布:入户门廊面积计算法 编辑:程序博客网 时间:2024/05/08 00:05

《The C programming Language》中这样解释stdin,stdout

"The file pointers stdin and stdout are objects of type FILE *. They are constants, however, not variables, so it is not possible to assign to them."

stdout标准输出设备的文件句柄宏定义printf其实就是fprintf的第一个参数设置为stdout你可以理解为它就是一个文件,而这个文件和标准输出设备(屏幕)建立了某种关联,当数据写到这个文件里面的时候,屏幕就会通过既定的方式把你写进去的东西显示出来.在C程序中完全可以讲stdout当做一种文件结构来处理。

#include <stdio.h>int main(int argc,char *argv[]){FILE *ifile;//FILE *ofile;char c;if(argc <= 1){return 0;}else{if((ifile = fopen(*++argv,"r")) == NULL){printf("cat the file %s erro.\n",*argv);return 0;}else{while((c = getc(ifile)) != EOF){putc(c,stdout);}}}return 0;} 


 

原创粉丝点击