I have a table like this;
01 01 02 00 01 02 00 01
01 02 00 01 02 00 01 02
02 00 11 04 04 04 04 04
00 01 10 03 03 03 03 03
I am reading this file like this:
FILE *map = open_file("lazy.map");
if (map == 0)
return 0;
for (i = 0; i < TOTAL_TILES; ++i) {
int ttype;
fscanf(map,"%i",&ttype);
// do other stuff here...
}
Is there a way that I can check if fscanf failed or not?
Check the return value.
Another option is to use
%nformat:This way you can check how many symbols have been consumed by
fscanf. It doesn’t make difference in the above, but in:countmay be greater than one even ifabcpart of the format didn’t match. On the other hand,nwill be set only if the whole format matched.