* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes / sx12a.html
blob976f533076d167e98163350669dc6bfce4a8a25f
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.1 File Pointers and <TT>fopen</TT></title>
10 <link href="sx12.html" rev=precedes>
11 <link href="sx12b.html" rel=precedes>
12 <link href="sx12.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>12.1 File Pointers and <TT>fopen</TT></H2>
17 <p>[This section corresponds to K&amp;R Sec. 7.5]
18 </p><p>How will we specify that we want to access a particular data file?
19 It would theoretically be possible
20 to mention the name of a file
21 each time it was desired to read from or write to it.
22 But such an approach would have a number of drawbacks.
24 Instead,
25 the usual approach
26 (and the one taken in C's stdio library)
27 is that you mention the name of the file once,
28 at the time you <dfn>open</dfn> it.
29 Thereafter, you use some little
30 token--in
31 this case,
32 the <dfn>file pointer</dfn>--which
33 keeps track
34 (both for your sake and the library's)
35 of which file you're talking about.
36 Whenever you want to
37 read from or write to
38 one of the files you're working with,
39 you identify that file
40 by using its file pointer
41 (that is, the file pointer you obtained when you opened the file).
42 As we'll see, you store file pointers in variables
43 just as you store any other data you manipulate,
46 is possible to have several files open,
47 as long as you use distinct variables to store the file pointers.
48 </p><p>You declare a variable to store a file pointer like this:
49 <pre>
50 FILE *fp;
51 </pre>
52 The type <TT>FILE</TT> is predefined for you by
53 <TT>&lt;stdio.h&gt;</TT>.
54 It is a data structure which holds the information
55 the standard I/O library needs to keep track of
56 the file for you.
57 For historical reasons,
58 you declare a variable which is a pointer to this <TT>FILE</TT> type.
59 The name of the variable can
60 (as for any variable)
61 be anything you choose;
62 it is traditional to use the letters <TT>fp</TT> in the variable name
63 (since we're talking about a <TT>f</TT>ile <TT>p</TT>ointer).
64 If you were reading from two files at once
65 you'd probably use two file pointers:
66 <pre>
67 FILE *fp1, *fp2;
68 </pre>
69 If you were reading from one file and writing to another
70 you might declare
71 and input file pointer and an output file pointer:
72 <pre>
73 FILE *ifp, *ofp;
74 </pre>
75 </p><p>Like any pointer variable,
76 a file pointer isn't any good until it's initialized to point to something.
77 (Actually, no variable of any type is much good until you've initialized it.)
78 To actually open a file,
79 and receive the ``token''
80 which you'll store in your file pointer variable,
81 you call <TT>fopen</TT>.
82 <TT>fopen</TT> accepts a file name (as a string)
83 and a <dfn>mode</dfn> value
84 indicating among other things whether you intend to read or write this file.
85 (The mode variable is also a string.)
86 To open the file <TT>input.dat</TT> for reading you might call
87 <pre>
88 ifp = fopen("input.dat", "r");
89 </pre>
90 The mode string <TT>"r"</TT> indicates reading.
91 Mode <TT>"w"</TT> indicates writing,
92 so we could open <TT>output.dat</TT> for
94 output like this:
95 <pre>
96 ofp = fopen("output.dat", "w");
97 </pre>
98 The other values for the mode string are less frequently used.
99 The third major mode is <TT>"a"</TT> for append.
100 (If you use <TT>"w"</TT> to write to a file which already exists,
101 its old contents will be discarded.)
102 You may also add a <TT>+</TT> character
103 to the mode string
104 to indicate that you want to both read and write,
105 or a <TT>b</TT> character
106 to indicate that you want to do ``binary''
107 (as opposed to text)
108 I/O.
109 </p><p>One thing to beware of when opening files
110 is that it's an operation which may fail.
111 The requested file might not exist,
112 or it might be protected against reading or writing.
113 (These possibilities ought to be obvious,
114 but it's easy to forget them.)
115 <TT>fopen</TT> returns a null pointer if it can't open the requested file,
116 and it's important to check for this case
117 before going off and using <TT>fopen</TT>'s return value as a file pointer.
118 Every call to <TT>fopen</TT> will typically be followed with a test,
119 like this:
120 <pre>
121 ifp = fopen("input.dat", "r");
122 if(ifp == NULL)
124 printf("can't open file\n");
125 <I>exit or return</I>
127 </pre>
128 If <TT>fopen</TT> returns a null pointer,
129 and you store it in your file pointer variable
130 and go off and try to do I/O with it,
131 your program will typically crash.
132 </p><p>It's common to collapse the call to <TT>fopen</TT>
134 the assignment
135 in with
136 the test:
138 <pre>
139 if((ifp = fopen("input.dat", "r")) == NULL)
141 printf("can't open file\n");
142 <I>exit or return</I>
144 </pre>
145 You don't have to write these ``collapsed'' tests
146 if you're not comfortable with them,
147 but you'll see them in other people's code,
148 so you should be able to read them.
149 </p><hr>
151 Read sequentially:
152 <a href="sx12.html" rev=precedes>prev</a>
153 <a href="sx12b.html" rel=precedes>next</a>
154 <a href="sx12.html" rev=subdocument>up</a>
155 <a href="top.html">top</a>
156 </p>
158 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
159 // <a href="copyright.html">Copyright</a> 1995-1997
160 // <a href="mailto:scs@eskimo.com">mail feedback</a>
161 </p>
162 </body>
163 </html>