man-pages/man3/stdin.3

156 lines
3.9 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-08, mtk, Converted from mdoc to man macros
2007-12-03 19:53:53 +00:00
.\"
.TH STDIN 3 2008-07-14 "Linux" "Linux Programmer's Manual"
2007-12-03 19:53:53 +00:00
.SH NAME
stdin, stdout, stderr \- standard I/O streams
.SH SYNOPSIS
.nf
.B #include <stdio.h>
2007-12-23 08:20:39 +00:00
.BI "extern FILE *" stdin ;
.BI "extern FILE *" stdout ;
.BI "extern FILE *" stderr ;
2007-12-03 19:53:53 +00:00
.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
2008-06-28 04:57:20 +00:00
the parent process chose to set up.
(See also the "Redirection" section of
2007-12-03 19:53:53 +00:00
.BR sh (1).)
.PP
2007-12-03 19:53:53 +00:00
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)
2007-12-16 13:39:24 +00:00
macro of type pointer to
.IR 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).
.PP
2007-12-16 13:39:24 +00:00
Since
.IR FILE s
are a buffering wrapper around Unix file descriptors, the
2004-11-03 13:51:07 +00:00
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).
.PP
On program startup, the integer file descriptors
associated with the streams
.IR stdin ,
2007-12-03 19:53:53 +00:00
.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.)
.PP
2007-12-16 13:39:24 +00:00
Note that mixing use of
.IR FILE s
and raw file descriptors can produce
2004-11-03 13:51:07 +00:00
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.
.PP
2004-11-03 13:51:07 +00:00
Since the symbols
.IR stdin ,
2007-12-03 19:53:53 +00:00
.IR stdout ,
2004-11-03 13:51:07 +00:00
and
2007-12-03 19:53:53 +00:00
.I stderr
are specified to be macros, assigning to them is nonportable.
2004-11-03 13:51:07 +00:00
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.
.SH "CONFORMING TO"
The
.IR stdin ,
.IR stdout ,
and
.I stderr
macros conform to C89
and this standard also stipulates that these three
streams shall be open at program startup.
.SH NOTES
2004-11-03 13:51:07 +00:00
The stream
.I stderr
is unbuffered.
The stream
.I 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
.I 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 SEE ALSO
.BR csh (1),
.BR sh (1),
2007-12-03 19:53:53 +00:00
.BR open (2),
.BR fopen (3),
.BR stdio (3)