in reply to Perl binary file reading

Without seeing the code, we can't help you. If you wrote it the way you should, it should have worked.

Note that unpack might be more suitable to extract the values than substr.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Perl binary file reading
by kepler (Scribe) on May 02, 2016 at 19:51 UTC
    Hi, Sorry. Here's the code:
    my $filename = "Words.txt"; open(my $fh, '<', $filename) or die "Could not open file '$filename' $ +!"; binmode $fh; my $data = <$fh>; $data =~ s/\x00/\ /gi; close $fh; my $length = length($data); my $count = 0; for(my $i=0;$i < $length/36;$i++){ my $name = substr($data,$i*36,28); my $book = ord(substr($data,$i*36 + 28,1)) + 1; my $v1 = ord(substr($data,$i*36 + 29,1)) + 1; my $v2 = ord(substr($data,$i*36 + 30,1)) + 1; print $name . " - " . "$book\:$v1\:$v2\n"; $count += 1; }
    Stops after 38 records - it should go up to some thousands... The lenght says its about only 1300 characters - it should be also some thousands... Regards, Kepler
      > my $data = <$fh>;

      Do you know what implements the diamond operator? readline! You're reading up to the first newline only, binmode doesn't change this. Use read instead.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Hi, Sadly, "read" doesn't work for me... but this does :)
        my @r = <$fh>; my $data = join '', @r;
        Thanks anyway guys. You're great. Regards. Kepler
      What do you get if you try this on the file?
      #!/usr/bin/perl use strict; use warnings; my $fname = "Words.txt"; my $fsize = -s $fname; open( my $fh, '<', $fname ) or die "$fname: $!\n"; binmode $fh; $/ = undef; my $data = <$fh>; my $length = length( $data ); my $nnulls = tr/\x00/ /; print "Read $length of $fsize bytes, changed $nnulls nulls to spaces\n +";
      That will just report the basic numbers to see whether you've read the entire file, and how many null bytes it contains.

      As for the stuff you're doing with ord(substr(...)) + 1; ... that looks like the sort of thing that should be done when you still have null bytes in the file (i.e. without converting the nulls to spaces). And it looks like the sort of thing you should be doing with unpack, as others have mentioned.