254 lines
6.2 KiB
HTML
254 lines
6.2 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of STDIN</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>STDIN</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
stdin, stdout, stderr - standard I/O streams
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>></B>
|
|
|
|
<B>extern FILE *</B><I>stdin</I><B>;</B>
|
|
<B>extern FILE *</B><I>stdout</I><B>;</B>
|
|
<B>extern FILE *</B><I>stderr</I><B>;</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?4+tty">tty</A></B>(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
|
|
<B><A HREF="/cgi-bin/man/man2html?1+sh">sh</A></B>(1).)
|
|
|
|
<P>
|
|
|
|
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
|
|
<I>stdin</I>,
|
|
|
|
<I>stdout</I>,
|
|
|
|
and
|
|
<I>stderr</I>.
|
|
|
|
<P>
|
|
|
|
Each of these symbols is a
|
|
<B><A HREF="/cgi-bin/man/man2html?3+stdio">stdio</A></B>(3)
|
|
|
|
macro of type pointer to
|
|
<I>FILE</I>,
|
|
|
|
and can be used with functions like
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fprintf">fprintf</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fread">fread</A></B>(3).
|
|
|
|
<P>
|
|
|
|
Since
|
|
<I>FILE</I>s
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lseek">lseek</A></B>(2).
|
|
|
|
<P>
|
|
|
|
On program startup, the integer file descriptors
|
|
associated with the streams
|
|
<I>stdin</I>,
|
|
|
|
<I>stdout</I>,
|
|
|
|
and
|
|
<I>stderr</I>
|
|
|
|
are 0, 1, and 2, respectively.
|
|
The preprocessor symbols
|
|
<B>STDIN_FILENO</B>,
|
|
|
|
<B>STDOUT_FILENO</B>,
|
|
|
|
and
|
|
<B>STDERR_FILENO</B>
|
|
|
|
are defined with these values in
|
|
<I><<A HREF="file:///usr/include/unistd.h">unistd.h</A>></I>.
|
|
|
|
(Applying
|
|
<B><A HREF="/cgi-bin/man/man2html?3+freopen">freopen</A></B>(3)
|
|
|
|
to one of these streams can change the file descriptor number
|
|
associated with the stream.)
|
|
<P>
|
|
|
|
Note that mixing use of
|
|
<I>FILE</I>s
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exec">exec</A></B>(3),
|
|
|
|
the child inherits all open file descriptors, but all old streams
|
|
have become inaccessible.
|
|
<P>
|
|
|
|
Since the symbols
|
|
<I>stdin</I>,
|
|
|
|
<I>stdout</I>,
|
|
|
|
and
|
|
<I>stderr</I>
|
|
|
|
are specified to be macros, assigning to them is nonportable.
|
|
The standard streams can be made to refer to different files
|
|
with help of the library function
|
|
<B><A HREF="/cgi-bin/man/man2html?3+freopen">freopen</A></B>(3),
|
|
|
|
specially introduced to make it possible to reassign
|
|
<I>stdin</I>,
|
|
|
|
<I>stdout</I>,
|
|
|
|
and
|
|
<I>stderr</I>.
|
|
|
|
The standard streams are closed by a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
|
|
|
|
and by normal program termination.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
The
|
|
<I>stdin</I>,
|
|
|
|
<I>stdout</I>,
|
|
|
|
and
|
|
<I>stderr</I>
|
|
|
|
macros conform to C89
|
|
and this standard also stipulates that these three
|
|
streams shall be open at program startup.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The stream
|
|
<I>stderr</I>
|
|
|
|
is unbuffered.
|
|
The stream
|
|
<I>stdout</I>
|
|
|
|
is line-buffered when it points to a terminal.
|
|
Partial lines will not
|
|
appear until
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
|
|
|
|
is called, or a newline is printed.
|
|
This can produce unexpected
|
|
results, especially with debugging output.
|
|
The buffering mode of the standard streams (or any other stream)
|
|
can be changed using the
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setbuf">setbuf</A></B>(3)
|
|
|
|
or
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setvbuf">setvbuf</A></B>(3)
|
|
|
|
call.
|
|
Note that in case
|
|
<I>stdin</I>
|
|
|
|
is associated with a terminal, there may also be input buffering
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?3+tcsetattr">tcsetattr</A></B>(3);
|
|
|
|
see also
|
|
<B><A HREF="/cgi-bin/man/man2html?1+stty">stty</A></B>(1),
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+termios">termios</A></B>(3).
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+csh">csh</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+sh">sh</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+stdio">stdio</A></B>(3)
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">CONFORMING TO</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">NOTES</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">SEE ALSO</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:57 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|