Does a blank exist after the last value on each line?
If the line ends with '0\n'
instead of '0 \n'
, the invocation of fgetc
in the inner loop will read the \n
the first time the loop terminates, and the outer loop will read the next character. This will cause your arithmetic to result in ' ' - '0'
many times, which is 32-48 = -16
on a system that uses a character encoding compatible with ASCII.
Similarly, '\n' - '0'
is 10-48 = -38
. Your problem is that the last number on each line is not followed by a blank.
Either output a blank before the \n
when you save your game board or remove the last invocation of fgetc
since the inner loop already reads the \n
.
Edit: As cnicutar responded, you should also be checking your return values.