stdout,stdin淺析

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


 

發佈了46 篇原創文章 · 獲贊 102 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章