zoukankan      html  css  js  c++  java
  • C语言中标准输入流、标准输出流、标准错误输出流

    在Linux中,所有对设备和文件的操作都使用文件描述符来进行。

    Linux中一个进程启动时,都会打开3个文件:标准输入、标准输出和标准出错处理。这三个文件分别对应文件描述符0、1、2。

    在C语言中,在程序开始运行时,系统自动打开3个标准文件:标准输入、 标准输出、标准出错输出。通常这3个文件都与终端相联系。因此,以前我们所用到的从终端输入或输出都不需要打开终端文件。系统自定义了3个文件指针stdin、stdout、stderr,分别指向终端输入、终端输出和标准出错输出(也从终端输出)。

    标准输入流:stdin

    标准输出流:stdout

    标准错误输出流:stderr

    stdin


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

    stdout


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

    stderr


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

    perror


    function
    <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.
  • 相关阅读:
    struts2配置文件详解
    实体类和数据库映射--配置文件
    Hibernate常用配置文件详解
    本地计算机上的OracleOraDb11g_home1TNSListener服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。——Oracle监听器服务无法启动!
    关于远程访问Oracle数据库的设置(共享数据库)
    Log4J日志配置详解
    Java compiler level does not match the version of the installed Java project facet.问题
    Genymotion出现Unable to load VirtualBox engine问题--100%解决
    eclipse注解——作者,创建时间,版本
    web工程下的html中引用其他目录下的文件的path
  • 原文地址:https://www.cnblogs.com/younes/p/1650812.html
Copyright © 2011-2022 走看看