* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx12c.html
blob8088c79be3fd6908d9c84a0d75cee040d085231f
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>12.3 Predefined Streams</title>
10 <link href="sx12b.html" rev=precedes>
11 <link href="sx12d.html" rel=precedes>
12 <link href="sx12.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>12.3 Predefined Streams</H2>
17 <p>Besides the file pointers which we explicitly open by calling <TT>fopen</TT>,
18 there are also three predefined streams.
19 <TT>stdin</TT> is a constant file pointer corresponding to standard input,
20 and <TT>stdout</TT> is
21 a constant file pointer corresponding to standard output.
22 Both of these can be used anywhere a file pointer is called for;
23 for example,
24 <TT>getchar()</TT> is the same as <TT>getc(stdin)</TT>
25 and <TT>putchar(c)</TT> is the same as <TT>putc(c, stdout)</TT>.
26 The third predefined stream is <TT>stderr</TT>.
27 Like <TT>stdout</TT>,
28 <TT>stderr</TT> is typically connected to the screen by default.
29 The difference is that <TT>stderr</TT> is not redirected
30 when the standard output is redirected.
31 For example,
32 under Unix or MS-DOS,
33 when you invoke
34 <pre>
35 program &gt; filename
36 </pre>
37 anything printed to <TT>stdout</TT>
38 is redirected to the file <TT>filename</TT>,
39 but anything printed to <TT>stderr</TT> still goes to the screen.
40 The intent behind <TT>stderr</TT> is
41 that it is the ``standard error output'';
42 error messages printed to it will not disappear into an output file.
43 For example,
44 a more realistic way to print an error message
45 when a file can't be opened would be
46 <pre>
47 if((ifp = fopen(filename, "r")) == NULL)
49 fprintf(stderr, "can't open file %s\n", filename);
50 <I>exit or return</I>
52 </pre>
53 where <TT>filename</TT> is a string variable
54 indicating the file name to be opened.
55 Not only is the error message printed to <TT>stderr</TT>,
56 but it is also more informative
57 in that it mentions the name of the file that couldn't be opened.
58 (We'll see another example in the next chapter.)
60 </p><hr>
61 <p>
62 Read sequentially:
63 <a href="sx12b.html" rev=precedes>prev</a>
64 <a href="sx12d.html" rel=precedes>next</a>
65 <a href="sx12.html" rev=subdocument>up</a>
66 <a href="top.html">top</a>
67 </p>
68 <p>
69 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
70 // <a href="copyright.html">Copyright</a> 1995-1997
71 // <a href="mailto:scs@eskimo.com">mail feedback</a>
72 </p>
73 </body>
74 </html>