Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Missing bytes??

by stevee (Acolyte)
on Apr 06, 2008 at 15:32 UTC ( #678641=perlquestion: print w/replies, xml ) Need Help??

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

Hi Venerable Monks, I am struggling with a corrupt zip archive (another story really!) but to help me in wading through the analysis I have written the following code:
open (FILE, "< i:\\"); open (OUT, "> i:\\fullhexdump.txt"); binmode(FILE); $count=0; while (<FILE>) { $count++; read(FILE, $buf, 1); $byte = unpack "H*", $buf; print "$byte"; print OUT "$byte "; } close FILE; close OUT; print "\n\n$count\n\n";
Now the zip archive is 5,914,271KB big, but running the above the count only gets to 22,780,549 bytes (giving a text output file of length 66,740KB). Obviously I am not reading the whole file in, but I can not see why. Does any Learned Brother or Sister have any idea?

Replies are listed 'Best First'.
Re: Missing bytes??
by Corion (Patriarch) on Apr 06, 2008 at 15:36 UTC

    You are reading from the file in two locations:

    while (<FILE>) { ... read(FILE, $buf, 1);

    More likely you want while (! eof FILE) { instead of reading a line from FILE and then reading another byte from FILE.

      while (read(FILE, $buf, 1)) { ... }
      Thanks Corion, trying it now. Sadly I did not realise that while (<FILE>) read the file too!
        interesting, what did you think ?
Re: Missing bytes??
by jwkrahn (Monsignor) on Apr 06, 2008 at 18:19 UTC

    You don't have to read only one byte at a time:

    open my $FILE, '<:raw', 'i:/' or die "Cannot open + 'i:/' $!"; open my $OUT, '>', 'i:/fullhexdump.txt' or die "Cannot open 'i:/fullhe +xdump.txt' $!"; $/ = \2048; my $count; while ( <$FILE> ) { $count += length; print $OUT unpack 'H*', $_; } close $FILE; close $OUT; print "\n\n$count\n\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://678641]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2022-06-28 18:54 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (92 votes). Check out past polls.