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>12.5 Example: Reading a Data File
</title>
10 <link href=
"sx12d.html" rev=precedes
>
11 <link href=
"sx13.html" rel=precedes
>
12 <link href=
"sx12.html" rev=subdocument
>
15 <H2>12.5 Example: Reading a Data File
</H2>
17 <p>Suppose you had a data file consisting of rows and columns of numbers:
23 Suppose you wanted to read these numbers into an array.
24 (Actually, the array will be an array of arrays,
25 or a ``multidimensional'' array;
29 We can write code to do this by putting together several pieces:
30 the
<TT>fgetline
</TT> function we just showed,
31 and the
<TT>getwords
</TT> function from
34 Assuming that the data file is named
<TT>input.dat
</TT>,
35 the code would look like this:
41 int array[MAXROWS][MAXCOLS];
42 char *filename =
"input.dat";
50 ifp = fopen(filename,
"r");
53 fprintf(stderr,
"can't open %s\n", filename);
57 while(fgetline(ifp, line, MAXLINE) != EOF)
59 if(nrows
>= MAXROWS)
61 fprintf(stderr,
"too many rows\n");
65 n = getwords(line, words, MAXCOLS);
67 for(i =
0; i
< n; i++)
68 array[nrows][i] = atoi(words[i]);
72 Each trip through the loop reads one line from the file,
73 using
<TT>fgetline
</TT>.
74 Each line is broken up into ``words'' using
<TT>getwords
</TT>;
75 each ``word'' is actually one number.
76 The numbers are however still represented as strings,
77 so each one is converted to an
<TT>int
</TT> by calling
<TT>atoi
</TT>
78 before being stored in the array.
79 The code checks for two different error conditions
80 (failure to open the input file,
81 and too many lines in the input file)
82 and if one of these conditions occurs,
83 it prints an error message, and exits.
84 The
<TT>exit
</TT> function is a Standard library function
85 which terminates your program.
86 It is declared in
<TT><stdlib.h
></TT>,
87 and accepts one argument,
88 which will be the
<dfn>exit status
</dfn> of the program.
89 <TT>EXIT_FAILURE
</TT> is a code,
91 <TT><stdlib.h
></TT>,
92 which indicates that the program failed.
93 Success is indicated by a code of
<TT>EXIT_SUCCESS
</TT>, or simply
0.
94 (These values can also be returned from
<TT>main()
</TT>;
95 calling
<TT>exit
</TT> with a particular status value
97 essentially equivalent
98 to returning that same status value from
<TT>main
</TT>.)
102 <a href=
"sx12d.html" rev=precedes
>prev
</a>
103 <a href=
"sx13.html" rel=precedes
>next
</a>
104 <a href=
"sx12.html" rev=subdocument
>up
</a>
105 <a href=
"top.html">top
</a>
108 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
109 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
110 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>