What is NIL? Is that a macro for 0?
If you're checking the return value of fscanf, note that it returns the number of items it was able to successfully convert, or EOF if it reaches end of file while attempting. So it would be better to check for != 1 in this case.
Can you show a minimal compilable example that reproduces your issue?
Edit: Just to show you an complete example of fscanf
what is the type of c?
what is in the file, what do you *think* it should be?
its one of 2 things... you are not reading WHERE you think you are reading (where YOU think it is reading and where it IS reading mismatch), or you are not reading WHAT you think you are reading (type of data is not what you think, eg the file says 13 and you got '1' or you wanted 1 and got '1' or the like)
What is NIL? Is that a macro for 0?
its a C (and pascal) version of null. I am not sure if there is any difference, I think its all zero in the end..
he says its getting data and not tripping the if statement so its reading something. If the stream has gone invalid, that would also explain junk data... can the stream be bad and get a nonzero read from the fscanf? I am assuming nil=null=0 etc so even if nil isnt the right thing to check (its no pointer) I think its ok (??).
It also seems to be something in 'objective C' which I am not 100% sure what is.
I had to look it up, and got enough hits that it seems to be valid for some dialects.
given that the OP had it in all caps, though, it almost certainly is a local define for his code, not objective or other things. C would not use all caps for it even in an obscure dialect. I would still assume its a zero, but you may want to check it / print it / make sure.
Thanks for the various answers. Of course I didn't give all the necessary information. Here's a cut down version of the program with the parts that I think are needed:
“ifile” is a simple text file but the variable “c” in “if((fscanf(ifile, "%c", &c)) == NIL)” is not being being set to the correct value. Everything up to that statement works correctly.
adding a print to see what the value of c is, I still get the first character in my file.
its working.
add a printf to see what c is .. (how are YOU looking at it?)
good fun:
add the print in the second if statement too. What do these changes do for you..?
1 2 3 4 5 6 7 8 9
return(1) ;
if((fscanf(ifile,"%c",&c)) == NIL)
{
printf("oh no! : %c", c); //c will be random junk.
return(1) ;
}
printf("%c", c); //c will be the correct first letter in your text file.
return 0;