在Linux中,所有對設(shè)備和文件的操作都使用文件描述符來進(jìn)行。
Linux中一個進(jìn)程啟動時,都會打開3個文件:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)出錯處理。這三個文件分別對應(yīng)文件描述符0、1、2。
在C語言中,在程序開始運(yùn)行時,系統(tǒng)自動打開3個標(biāo)準(zhǔn)文件:標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)出錯輸出。通常這3個文件都與終端相聯(lián)系。因此,以前我們所用到的從終端輸入或輸出都不需要打開終端文件。系統(tǒng)自定義了3個文件指針 stdin、stdout、stderr,分別指向終端輸入、終端輸出和標(biāo)準(zhǔn)出錯輸出(也從終端輸出)。
標(biāo)準(zhǔn)輸入流:stdin
標(biāo)準(zhǔn)輸出流:stdout
標(biāo)準(zhǔn)錯誤輸出流:stderr
stdin
object
Standard input stream
The standard input stream is the default source of data for applications. It is usually directed to the input device of the standard console (generally, a keyboard).
stdin can be used as an argument for any function that expects an input stream as one of its parameters, like fgets or fscanf.
Although it is generally safe to assume that the source of data for stdin is going to be a keyboard, bear in mind that this may not be the case even in regular console systems, since stdin can be redirected at the operating system level. For example, many systems, among them DOS/Windows and most UNIX shells, support the following command syntax:
myapplication < example.txt
to use the content of the file example.txt as the primary source of data for myapplication instead of the console keyboard.
It is also possible to redirect stdin to some other source of data from within a program using the freopen function.
stdout
object
Standard output stream
The standard output stream is the default destination of regular output for applications. It is usually directed to the output device of the standard console (generally, the screen).
stdout can be used as an argument for any function that expects an output stream as one of its parameters, like fputs or fprintf.
Although it is generally safe to assume that the default destination for stdout is going to be the screen, bear in mind that this may not be the case even in regular console systems, since stdout can be redirected at the operating system level. For example, many systems, among them DOS/Windows and most UNIX shells, support the following command syntax:
myapplication > example.txt
to redirect the output of myapplication to the file example.txt instead of the screen.
It is also possible to redirect stdout to some other source of data from within a program using the freopen function.
相關(guān)推薦:計(jì)算機(jī)等考二級C:C中如何顯示*.bmp文件北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |