* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx12e.html
blob6d17a35207c4a182648f632f621b169ef63e2055
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.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>
13 </head>
14 <body>
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:
18 <pre>
19 1 2 34
20 5 6 78
21 9 10 112
22 </pre>
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;
26 see section
28 4.1.2.)
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
33 chapter 10.
34 Assuming that the data file is named <TT>input.dat</TT>,
35 the code would look like this:
36 <pre>
37 #define MAXLINE 100
38 #define MAXROWS 10
39 #define MAXCOLS 10
41 int array[MAXROWS][MAXCOLS];
42 char *filename = "input.dat";
43 FILE *ifp;
44 char line[MAXLINE];
45 char *words[MAXCOLS];
46 int nrows = 0;
47 int n;
48 int i;
50 ifp = fopen(filename, "r");
51 if(ifp == NULL)
53 fprintf(stderr, "can't open %s\n", filename);
54 exit(EXIT_FAILURE);
57 while(fgetline(ifp, line, MAXLINE) != EOF)
59 if(nrows &gt;= MAXROWS)
61 fprintf(stderr, "too many rows\n");
62 exit(EXIT_FAILURE);
65 n = getwords(line, words, MAXCOLS);
67 for(i = 0; i &lt; n; i++)
68 array[nrows][i] = atoi(words[i]);
69 nrows++;
71 </pre>
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>&lt;stdlib.h&gt;</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,
90 also defined by
91 <TT>&lt;stdlib.h&gt;</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>.)
99 </p><hr>
101 Read sequentially:
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>
106 </p>
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>
111 </p>
112 </body>
113 </html>