《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;
}