I have a piece of code that processes a file to get certain values out of it.
Basically the input file is formatted like this:
x y d
(over multiple lines)
x and y are positive integers and d is N, E, S or W.
In my code are a few bits relating to errors. If you think they're relevent, ask, because I don't want to fill up this post with too much un-needed information.
My code is as follows:
Code:
136 for(i = 1; i <= shipNumber; ++i) {
137 if(feof(mapFile)) {
138 return error(52);
139 }
140 fgets(line, 1024, mapFile);
141 if(input_too_long(line)) {
142 return error(52);
143 }
144 if(sscanf(line, "%d %d %c", &x, &y, &d) != 3) {
145 return error(52);
146 }
147 switch(d) {
148 case 'N':
149 case 'E':
150 case 'S':
151 case 'W':
152 break;
153 default:
154 return error(52);
155 }
156 for(j = 0; j < shipLengths[i-1]; ++j) {
157 if((x < 0) || (x >= columns) || (y < 0) || (y >= rows)) {
158 return error(51);
159 }
160 if(map[y][x] != '0') {
161 return error(50);
162 }
163 map[y][x] = (char)i;
164 switch(d) {
165 case 'N':
166 y--;
167 break;
168 case 'E':
169 x++;
170 break;
171 case 'S':
172 y++;
173 break;
174 case 'W':
175 x--;
176 }
177 }
178 }
The input file that is giving me problems is this:
Code:
0 0 E
4 7 E
5 0 E
2 1 E 2 2 E
EDIT: the line begining 2 1 E is the last line of the file. The board is making it look like there is a blank link on line 5 but there isn't.
Now, this should produce an error 52, as shipNumber is 5 and there are only 4 lines in the file. What happens (I have figured out through a bunch of printf statements I put in to debug) is after the fourth line is read, the next time through the loop, feof(mapFile) returns false (should return true!) and fgets(line, 1024, mapFile) reads the fourth line again.
What would cause my program to think it hasn't read line 4 yet?