stdin stdout stderr - 標準 I/O 流
SYNOPSIS 總覽Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr;
DESCRIPTION 描述通常,每個 Unix 程序在啟動時都會打開三個流,一個用於輸入,一個用於輸出,一個用於列印診斷或錯誤消息。典型的,他們被連接到用戶的終端 (參見 tty(4)) 但是也有可能指向文件或是其他設備,取決於父進程選擇設置了什麼 (參見 sh(1) 的重定向 (``Redirection'') 章節。)
輸入流被稱為 ``standard input''; 輸出流被稱為 ``standard output''; 錯誤流被稱為 ``standard error''。這些名詞通常簡寫為符號,用於引用這些文件,它們是 stdin stdout 和 stderr
這些符號中,每一個都是 stdio(3) 中的一個宏,類型是指向 FILE 的指針,可以用於類似 fprintf(3) 或 fread(3) 等函數中。
由於 FILE 是一個對 Unix 文件描述符加以緩衝的包裝,下層的文件也可以使用原始的 Unix 文件接口來存取。也就是,類似 read(2) 和 lseek(2) 的函數。與流 stdin stdout 和 stderr 關聯的整數形式的文件描述符分別是 0,1 還有 2。預處理器符號 STDIN_FILENO,STDOUT_FILENO 和 STDERR_FILENO 分別以它們為值,定義在 <unistd.h> 中。
注意混合使用 FILE 和原始的文件描述符可能帶來不可預料的結果,一般應當避免。(對於喜歡追根問底的人:POSIX.1 規範的 8.2.3 節詳細地描述了這樣的混合使用怎樣才能不出錯。) 一個簡單的規則是,文件描述符由內核控制,而 stdio 僅僅是一個庫。它的意思是,例如當調用 exec 之後,子進程可以繼承所有打開的文件描述符,但是任何原有的流都不可再存取了。
由於符號 stdin stdout 和 stderr 被指定為宏,為它們賦值將導致不可移植。利用庫函數 freopen(3) ,標準流可以用來指向不同的文件。引進這個函數專門用來為 stdin stdout 和 stderr 重新賦值。標準流在調用 exit(3) 和程序正常中止時被關閉。
SEE ALSO 參見sh(1), csh(1), open(2), fopen(3), stdio(3)
NAMEstdin stdout stderr - standard I/O streams
SYNOPSISFd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr;
DESCRIPTIONUnder normal circumstances every Unix program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user's terminal (see tty(4)) but might instead refer to files or other devices, depending on what the parent process chose to set up. (See also the ``Redirection'' section of sh(1).)
The input stream is referred to as ``standard input''; the output stream is referred to as ``standard output''; and the error stream is referred to as ``standard error''. These terms are abbreviated to form the symbols used to refer to these files, namely stdin stdout and stderr
Each of these symbols is a stdio(3) macro of type pointer to FILE, and can be used with functions like fprintf(3) or fread(3).
Since FILEs are a buffering wrapper around Unix file descriptors, the same underlying files may also be accessed using the raw Unix file interface, that is, the functions like read(2) and lseek(2). The integer file descriptors associated with the streams stdin stdout and stderr are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are defined with these values in <unistd.h>.
Note that mixing use of FILEs and raw file descriptors can produce unexpected results and should generally be avoided. (For the masochistic among you: POSIX.1, section 8.2.3, describes in detail how this interaction is supposed to work.) A general rule is that file descriptors are handled in the kernel, while stdio is just a library. This means for example, that after an exec, the child inherits all open file descriptors, but all old streams have become inaccessible.
Since the symbols stdin stdout and stderr are specified to be macros, assigning to them is non-portable. The standard streams can be made to refer to different files with help of the library function freopen(3), specially introduced to make it possible to reassign stdin stdout and stderr The standard streams are closed by a call to exit(3) and by normal program termination.
SEE ALSOsh(1), csh(1), open(2), fopen(3), stdio(3)
【責任編輯:
韓亞珊TEL:(010)68476606】