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. -->
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
>
15 <H2>16.1: Files and Streams
</H2>
17 <p>Since the beginning,
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'';
25 (for an interactive program)
26 to the user's keyboard and screen, respectively.
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><</TT> or
<TT>></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>.
45 it's also possible to open files
46 (or other I/O sources)
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>,
55 ``pointer to
<TT>FILE
</TT>,''
56 where
<TT>FILE
</TT> is a special type
57 defined by
<TT><stdio.h
></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><stdio.h
></TT>,
78 and can be used wherever a
<TT>FILE *
</TT> is required.
80 we could simulate--or,
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>.
88 <TT>stderr
</TT> is typically connected to the same
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.
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.
102 it's customary to print error messages
104 or anything else that shouldn't be redirected)
106 often by calling
<TT>fprintf(stderr, message,
</TT>...
<TT>)
</TT>.
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>
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>