fgetc() skips carriage returns?
I am reading a file which is a map file for my snake clone which I am writing in SDL and creating it from scratch. The field consists of 40x40 blocks and so the map file consists of 40 rows and 40 characters on each row. I wanted the program to be able to read the file for the three different line endings, LF, CRLF and CR but I kept getting errors which I tracked down what seems to be fgetc()'s inability to read CRs.
I had the program write the character in hex and the current column for each iteration to stderr.
The output from stderr looks like this in my text editor:
Code:
1: 0x2E 0
2: 0x2E 1
3: 0x2E 2
...
39: 0x2E 38
40: 0x2E 39
41: 0x2E 40
42: 0x2E 0
43: 0x2E 1
44: 0x2E 2
The CR should be at 40, but instead it skips to the next character.
The code which parses newlines:
Code:
if (i == 40) {
if (!(c == '\r' || c == '\n')) {
fprintf(stderr, "Error: Parse error; expected newline in map file on line %d. Got ascii code 0x%02X.", j+1, c);
DIE(1);
}
if (c == '\r') { //For Windows newlines
if ((c = fgetc(map)) != '\n') {
ungetc(c, map);
}
}
j++;
i = 0;
}
else
i++;
So the current code can only handle LF and CRLF (since it skips CR, it will think of CRLF as LF).
Is this a problem or will it work when compiled in an environment that uses CR for line endings?