why is it not working with ch?
this question is for salem for his first post.
Last edited by shubham; 05-30-2011 at 02:29 PM.
EOF is guaranteed to be a negative number outside the range of char. So you squish it back and forth trying to fit it into a byte and then promote it to compare it with something bigger than a byte.
Let's pretend that instead of bytes, the only thing you can read are single bits. EOF is defined in our example as 10 in binary, and all you can read is 0 or 1.Ok, so our file is: 001100 in bits. Then it is EOF. What happens in this comparison:Code:do bit = readbit( file ); /* read 1 or 0 */ while( bit != 10 ); /* promote a single bit into two bits to compare */
0 is read, 0 is promoted to 00 and compared with 10
0 is read, 0 is promoted to 00 and compared with 10
1 is read, 1 is promoted to 01 and compared with 10
1 is read, 1 is promoted to 01 and compared with 10
0 is read, 0 is promoted to 00 and compared with 10
0 is read, 0 is promoted to 00 and compared with 10
10 is read, squished into 0, promoted back to 00 and compared with 10
You have just read the EOF marker, but your program doesn't know you are at EOF, so you merrily read off past the end of your file until your program crashes or something else odd happens. In any event, you have no idea when you've reached the end of your file.
Quzah.
Hope is the first step on the road to disappointment.
i think it's value is -1 ? but i am not sure. that's why i seek your help quzah
Listen to what Quzah is telling us... it could be *any value* that is not a valid ascii or ansi character...
If you test for -1 and it gets recompiled on a system where it's -128... what happens?
That is one of the reason we use constants like EOF or MAX_INT in C... because those values can and do change from one compiler to the next.