C語言中標準輸入資料流、標準輸出資料流、標準錯誤輸出資料流

來源:互聯網
上載者:User
文章目錄
  • Parameters.

在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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.