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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.