begae60 has asked for the wisdom of the Perl Monks concerning the following question:

Hey,

With this code I try to read in a .bin file:
open FILE, "file.bin" or die; binmode(FILE); while ($ch = getc(FILE)){ $temp = ord($ch); print "$temp\n"; } close FILE;
But, after a couple of characters, the perl script stops to read in and exit! I took a look with a hex-editor, but I saw nothing like a EOF character... I upload the .bin file here: http://begae.baghack.ch/file.bin, in fact you wanna take a look at it.

Thanks in advance for helping me out.

Replies are listed 'Best First'.
Re: Unable to read in a .bin file.
by Tanktalus (Canon) on Aug 06, 2006 at 13:53 UTC

    That's because you have what's commonly known as a "nul" byte in that file. A nul byte has a value of zero, and is probably being treated by perl as false. What you want instead is not to rely on what getc returns, but merely on whether getc returns something that is defined or not (it returns undef at the end):

    while (defined(my $ch = getc(FILE))) {
    That solves the problem.

Re: Unable to read in a .bin file.
by davido (Cardinal) on Aug 06, 2006 at 15:23 UTC

    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

Re: Unable to read in a .bin file.
by jwkrahn (Abbot) on Aug 06, 2006 at 16:32 UTC
    As others have pointed out when $ch gets the character '0' (zero) the while expression is false and the loop ends. Another way to do what you want:
    $/ = \1; # Set Input Record Separator to one byte while ( my $ch = <FILE> ) {
    This will work no matter what the value of $ch is.