stdin 中文man页面

系统
通常,每个 Unix 程序在启动时都会打开三个流,一个用于输入,一个用于输出,一个用于打印诊断或错误消息。典型的,他们被连接到用户的终端 (参见 tty(4)) 但是也有可能指向文件或是其他设备,取决于父进程选择设置了什么 (参见 sh(1) 的重定向 (``Redirection'') 章节。)

NAME

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 stdoutstderr

这些符号中,每一个都是 stdio(3) 中的一个宏,类型是指向 FILE 的指针,可以用于类似 fprintf(3) 或 fread(3) 等函数中。

由于 FILE 是一个对 Unix 文件描述符加以缓冲的包装,下层的文件也可以使用原始的 Unix 文件接口来存取。也就是,类似 read(2) 和 lseek(2) 的函数。与流 stdin stdoutstderr 关联的整数形式的文件描述符分别是 0,1 还有 2。预处理器符号 STDIN_FILENO,STDOUT_FILENO 和 STDERR_FILENO 分别以它们为值,定义在 <unistd.h> 中。

注意混合使用 FILE 和原始的文件描述符可能带来不可预料的结果,一般应当避免。(对于喜欢追根问底的人:POSIX.1 规范的 8.2.3 节详细地描述了这样的混合使用怎样才能不出错。) 一个简单的规则是,文件描述符由内核控制,而 stdio 仅仅是一个库。它的意思是,例如当调用 exec 之后,子进程可以继承所有打开的文件描述符,但是任何原有的流都不可再存取了。

由于符号 stdin stdoutstderr 被指定为宏,为它们赋值将导致不可移植。利用库函数 freopen(3) ,标准流可以用来指向不同的文件。引进这个函数专门用来为 stdin stdoutstderr 重新赋值。标准流在调用 exit(3) 和程序正常中止时被关闭。  

SEE ALSO 参见

sh(1), csh(1), open(2), fopen(3), stdio(3)  

#p#

NAME

stdin stdout stderr - standard I/O streams  

SYNOPSIS

Fd #include <stdio.h> Fd extern FILE *stdin; Fd extern FILE *stdout; Fd extern FILE *stderr;  

DESCRIPTION

Under 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 ALSO

sh(1), csh(1), open(2), fopen(3), stdio(3)  

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 17:53:26

stdio中文man

2011-08-25 17:58:41

stdout中文man

2011-08-25 17:24:54

puts中文man

2011-08-24 18:05:31

SHOW中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-16 10:42:30

rmmod中文man

2011-08-23 16:24:57

smbmnt中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-24 15:48:38

INSERT中文man

2011-08-24 16:45:14

mailaddr中文man

2011-08-25 09:40:49

UPDATE中文man

2011-08-24 15:11:15

explain中文man

2011-08-24 15:29:06

grant中文man
点赞
收藏

51CTO技术栈公众号