That's right. It's a logic error in your script.
while will continue looping as long as the conditional within its parens evaluates to 'true'. When the conditional evaluates to false, the loop stops. In Perl, 'false' is: undef, 0, a string '0', an empty string ( '' ), or an empty list () (see perlsyn). You're being tripped up by the return value of getc() returning something that evaluates to false, but that isn't undef (ie, 0).
Your conditional is not testing whether or not another byte is returned from getc(), it's testing whether or not the return value of getc() is 'true'. The way to test whether or not a byte was returned is to check whether getc() returned a value or undef; getc's docs state that it returns undef at the end of the file or if there's an error, either way at that point you're done. ;)
So the issue is how to test for undef, instead of testing for true/false values. That's done with defined, as in, "while( defined( $ch = getc(FILE) ) ) {.....".
Dave
In reply to Re: Unable to read in a .bin file.
by davido
in thread Unable to read in a .bin file.
by begae60
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |