文章目錄
在Linux中,所有對裝置和檔案的操作都使用檔案描述符來進行。
Linux中一個進程啟動時,都會開啟3個檔案:標準輸入、標準輸出和標準出錯處理。這三個檔案分別對應檔案描述符0、1、2。
在C語言中,在程式開始運行時,系統自動開啟3個標準檔案:標準輸入、 標準輸出、標準出錯輸出。通常這3個檔案都與終端相聯絡。因此,以前我們所用到的從終端輸入或輸出都不需要開啟終端檔案。系統自訂了3個檔案指標stdin、stdout、stderr,分別指向終端輸入、終端輸出和標準出錯輸出(也從終端輸出)。
標準輸入資料流:stdin
標準輸出資料流:stdout
標準錯誤輸出資料流:stderr
stdinobject<cstdio>
FILE * stdin;
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.
stdoutobject<cstdio>
FILE * stdout;
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.
stderrobject<cstdio>
FILE * stderr;
Standard error stream
The standard error stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console (generally, the screen).
stderr can be used as an argument for any function that expects an output stream as one of its parameters, like fputs or fprintf.
Although generally both stdout and stderr are associated with the same console output, applications may differentiate between what is sent to stdout and what to stderrfor the case that one of them is redirected. For example, it is frequent to redirect the regular output of a console program (stdout) to a file while expecting the error messages to keep appearing in the console screen.
It is also possible to redirect stderr to some other destination from within a program using the freopen function.
perrorfunction<cstdio>
void perror ( const char * str );
Print error message
Interprets the value of the global variable errno into a string and prints that string to stderr (standard error output stream, usually the screen), optionaly preceding it with the custom message specified in
str.
errno is an integral variable whose value describes the last error produced by a call to a library function. The error strings produced by perror depend on the developing platform and compiler.
If the parameter
str is not a null pointer,
str is printed followed by a colon (:) and a space. Then, whether
str was a null pointer or not, the generated error description is printed followed by a newline character ('\n').
perror should be called right after the error was produced, otherwise it can be overwritten in calls to other functions.
Parameters.
-
str
-
C string containing a custom message to be printed before the error message itself.
If it is a null pointer, no preceding custom message is printed, but the error message is printed anyway.
By convention, the name of the application itself is generally used as parameter.