* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes.int / sx2a.html
blob3450b4eef93f69ea9546e2e0028a38270f6bc80d
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>16.1: Files and Streams</title>
10 <link href="sx2.html" rev=precedes>
11 <link href="sx2b.html" rel=precedes>
12 <link href="sx2.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>16.1: Files and Streams</H2>
17 <p>Since the beginning,
18 we've been using
19 ``standard input''
20 and ``standard output,''
21 two predefined I/O streams which are available to every C program.
22 The disposition of these streams is left deliberately unclear:
23 the program can assume that they're connected to the ``right place'';
24 usually
25 (for an interactive program)
26 to the user's keyboard and screen, respectively.
27 However,
28 since a program typically doesn't know
29 exactly where they go,
30 it's possible to redirect them,
31 behind the program's back,
32 and thereby to apply a program to some noninteractive input
33 or to capture its output,
34 without rewriting the program or doing any special I/O programming.
35 (This ability is a cornerstone
36 of the Unix ``toolkit'' methodology.
37 In Unix and several other systems,
38 you can redirect the input or output of a program
39 as you invoke it from the shell command line
40 using the <TT>&lt;</TT> or <TT>&gt;</TT> characters.)
41 </p><p>Standard input is assumed by functions like <TT>getchar</TT>,
42 and standard output is assumed
43 by functions like <TT>putchar</TT> and <TT>printf</TT>.
44 </p><p>Of course,
45 it's also possible to open files
46 (or other I/O sources)
47 explicitly.
48 We can open files using the function <TT>fopen</TT>;
49 certain systems may also provide specialized ways
50 of opening streams connected to I/O devices
51 or set up in more exotic ways.
52 A successful call to <TT>fopen</TT>
53 returns a pointer of type <TT>FILE *</TT>,
54 that is,
55 ``pointer to <TT>FILE</TT>,''
56 where <TT>FILE</TT> is a special type
57 defined by <TT>&lt;stdio.h&gt;</TT>.
58 A <TT>FILE *</TT>
59 (also called ``file pointer'')
60 is the handle by which we refer to an I/O stream in C.
61 I/O functions which do not assume standard input or standard output
62 all accept a <TT>FILE *</TT> argument
63 telling them which stream to read from or write to.
64 (Examples are <TT>getc</TT>, <TT>putc</TT>, and <TT>fprintf</TT>.)
65 Notice that a file pointer
66 which has been opened on a file
67 is not the same thing as the file itself.
68 A file pointer is a data structure which helps us
69 access or manipulate the file.
70 </p><p>Occasionally it is necessary
71 to refer to the standard input or standard output
72 in a situation which calls for a general-purpose <TT>FILE *</TT>.
73 To handle these cases,
74 there are two predefined constants:
75 <TT>stdin</TT> and <TT>stdout</TT>.
76 Both of these are of type <TT>FILE *</TT>,
77 are declared in <TT>&lt;stdio.h&gt;</TT>,
78 and can be used wherever a <TT>FILE *</TT> is required.
79 (For example,
80 we could simulate--or,
81 in fact,
82 implement--<TT>getchar</TT>
83 as <TT>getc(stdin)</TT>.)
84 </p><p>There is also a third predefined stream,
85 the ``standard error output.''
86 It has its own constant, <TT>stderr</TT>.
87 By default,
88 <TT>stderr</TT> is typically connected to the same
89 output device
90 as <TT>stdout</TT>;
91 the difference between <TT>stdout</TT> and <TT>stderr</TT>
92 is that <TT>stderr</TT> is <em>not</em> redirected
93 when <TT>stdout</TT> is.
94 <TT>stderr</TT> is,
95 as its name implies,
96 intended for error messages:
97 if your program printed its error messages to <TT>stdout</TT>
98 (e.g. by calling <TT>printf</TT>),
99 they would disappear into the output file
100 if the user redirected the standard output.
101 Therefore,
102 it's customary to print error messages
103 (and also prompts,
104 or anything else that shouldn't be redirected)
105 to <TT>stderr</TT>,
106 often by calling <TT>fprintf(stderr, message, </TT>...<TT>)</TT>.
107 </p><hr>
109 Read sequentially:
110 <a href="sx2.html" rev=precedes>prev</a>
111 <a href="sx2b.html" rel=precedes>next</a>
112 <a href="sx2.html" rev=subdocument>up</a>
113 <a href="top.html">top</a>
114 </p>
116 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
117 // <a href="copyright.html">Copyright</a> 1996-1999
118 // <a href="mailto:scs@eskimo.com">mail feedback</a>
119 </p>
120 </body>
121 </html>