man-pages/man3/stdin.3

146 lines
3.8 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" From dholland@burgundy.eecs.harvard.edu Tue Mar 24 18:08:15 1998
.\"
.\" This man page was written in 1998 by David A. Holland
.\" and placed in the Public Domain. Polished a bit by aeb.
.\" 2005-06-16 mtk, mentioned freopen()
2004-11-03 13:51:07 +00:00
.\"
2007-12-03 19:53:53 +00:00
.\" 2007-12-02, mtk, Converted from mdoc to man macros
.\"
.TH STDIN 3 2007-12-02 "Linux" "Linux Programmer's Manual"
.SH NAME
stdin, stdout, stderr \- standard I/O streams
.SH SYNOPSIS
.nf
.B #include <stdio.h>
.B extern FILE *stdin;
.B extern FILE *stdout;
.B extern FILE *stderr;
.fi
.SH DESCRIPTION
2004-11-03 13:51:07 +00:00
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
2004-11-03 13:51:07 +00:00
the user's terminal (see
2007-12-03 19:53:53 +00:00
.BR tty (4)
2004-11-03 13:51:07 +00:00
but might instead refer to files or other devices, depending on what
2007-12-03 19:53:53 +00:00
the parent process chose to set up. (See also the "Redirection" section of
.BR sh (1).)
.sp
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
2004-11-03 13:51:07 +00:00
used to refer to these files, namely
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.IR stderr .
2004-11-03 13:51:07 +00:00
Each of these symbols is a
2007-12-03 19:53:53 +00:00
.BR stdio (3)
2004-11-03 13:51:07 +00:00
macro of type pointer to FILE, and can be used with functions like
2007-12-03 19:53:53 +00:00
.BR fprintf (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR fread (3).
.sp
2004-11-03 13:51:07 +00:00
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
2007-12-03 19:53:53 +00:00
.BR read (2)
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.BR lseek (2).
.sp
On program startup, the integer file descriptors
associated with the streams
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
are 0, 1, and 2, respectively.
2007-12-03 19:53:53 +00:00
The preprocessor symbols
.BR STDIN_FILENO ,
.BR STDOUT_FILENO ,
and
.B STDERR_FILENO
are defined with these values in
2007-06-20 21:53:34 +00:00
\fI<unistd.h>\fP.
(Applying
2007-12-03 19:53:53 +00:00
.BR freopen (3)
to one of these streams can change the file descriptor number
associated with the stream.)
2007-12-03 19:53:53 +00:00
.sp
2004-11-03 13:51:07 +00:00
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
2007-12-03 19:53:53 +00:00
.BR exec (3),
2005-10-20 15:11:10 +00:00
the child inherits all open file descriptors, but all old streams
have become inaccessible.
2007-12-03 19:53:53 +00:00
.sp
2004-11-03 13:51:07 +00:00
Since the symbols
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
2004-11-03 13:51:07 +00:00
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
2007-12-03 19:53:53 +00:00
.BR freopen (3),
2004-11-03 13:51:07 +00:00
specially introduced to make it possible to reassign
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.IR stderr .
2004-11-03 13:51:07 +00:00
The standard streams are closed by a call to
2007-12-03 19:53:53 +00:00
.BR exit (3)
2004-11-03 13:51:07 +00:00
and by normal program termination.
2007-12-03 19:53:53 +00:00
.SH CONSIDERATIONS
2004-11-03 13:51:07 +00:00
The stream
2007-12-03 19:53:53 +00:00
stderr is unbuffered.
The stream
2007-12-03 19:53:53 +00:00
stdout is line-buffered when it points to a terminal.
Partial lines will not
2004-11-03 13:51:07 +00:00
appear until
2007-12-03 19:53:53 +00:00
.BR fflush (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR exit (3)
is called, or a newline is printed.
This can produce unexpected
2004-11-03 13:51:07 +00:00
results, especially with debugging output.
The buffering mode of the standard streams (or any other stream)
can be changed using the
2007-12-03 19:53:53 +00:00
.BR setbuf (3)
2004-11-03 13:51:07 +00:00
or
2007-12-03 19:53:53 +00:00
.BR setvbuf (3)
2004-11-03 13:51:07 +00:00
call.
Note that in case
2007-12-03 19:53:53 +00:00
stdin is associated with a terminal, there may also be input buffering
2004-11-03 13:51:07 +00:00
in the terminal driver, entirely unrelated to stdio buffering.
(Indeed, normally terminal input is line buffered in the kernel.)
This kernel input handling can be modified using calls like
2007-12-03 19:53:53 +00:00
.BR tcsetattr (3);
2004-11-03 13:51:07 +00:00
see also
2007-12-03 19:53:53 +00:00
.BR stty (1),
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.BR termios (3).
.SH "CONFORMING TO"
2004-11-03 13:51:07 +00:00
The
2007-12-03 19:53:53 +00:00
.IR stdin ,
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
macros conform to C89
2004-11-03 13:51:07 +00:00
and this standard also stipulates that these three
streams shall be open at program startup.
2007-12-03 19:53:53 +00:00
.SH SEE ALSO
.BR sh (1),
.BR csh (1),
.BR open (2),
.BR fopen (3),
.BR stdio (3)