Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Problem on substr record on binary file

by bh_perl (Monk)
on Jun 25, 2010 at 06:59 UTC ( #846472=note: print w/replies, xml ) Need Help??


in reply to Re: Problem on substr record on binary file
in thread Problem on substr record on binary file

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Re^2: Problem on substr record on binary file

Replies are listed 'Best First'.
Re^3: Problem on substr record on binary file
by ikegami (Patriarch) on Jun 25, 2010 at 07:16 UTC

    Then why are you asking Perl to read until the next newline?

    Besides, your data has two newlines.

    84 47 00 0c 00 00 11 0a 03 50 35 04 00 64 0a 04 ^^ ^^ 16 12 00 1e 00 1d 00 00 65 09 08 54 52 03 2f 82 05 10 00 02 6e 06 0b a8 53 11 67 00 00 00 7a 7f 00 69 4a 42 47 48 4a 41 0c 00 6a 53 54 4d 44 54 4f 1c 00 66 04 00 00

    If you want to read the whole file at once, why do you have a loop? Undefine $/ and get rid of the loop.

    local $/; my $data = <DATA>;

    If you're not trying to read the entire file at once, what are you trying to do?

Re^3: Problem on substr record on binary file
by cdarke (Prior) on Jun 25, 2010 at 07:33 UTC
    How to read it correctly ?

    As moritz said, use read. This enables you to specify exactly how many bytes to read, which is exactly what you are looking for.

      Thank you very much for your help. This is my last coding after some modification based on your advise. Please guide me if I am wrong.
      #!/usr/bin/perl -w use Cwd; use warnings; use strict; use Getopt::Long; use constant FILEHDR => 4; use constant CDRLEN => 286; my ($trace, $help, $infile); my $swap = ''; my $indir = getcwd; my $outdir = getcwd; GetOptions ( "h|help" => \$help, "filename|f=s" => \$infile, "swap|s" => \$swap, "input|i=s" => \$indir, "output|o=s" => \$outdir, "trace|t" => \$trace ) or usage(); sub usage { exit; } my $outfile = $infile; my $data; if ($infile) { #open (OUTPUT, ">$outdir/$outfile"); open (DATA, "$indir/$infile"); binmode DATA; until (eof DATA) { read (DATA, $data, 2); my $tag = unpack "H2", substr $data,0,1,''; my $length = unpack "C", substr $data,0,1,''; $length -= 2; if ($length == "81") { $length = unpack "C", substr $data,0,1,''; $length -= 1; } if ($length > 0) { read (DATA, $data, $length); my $rec = unpack "H*", substr $data,0,$length,''; printf ("RECORD TAG : %s\n", $tag); printf ("RECORD LENGTH : %s\n", $length); printf ("RECORD : %s\n", $rec); } } close(DATA); #close(OUTPUT); }
        Without actually testing your code, that seems to be OK. A couple of things to improve it though: you should always test the return value from open,
        open (DATA, "$indir/$infile") or die "Unable to open $indir/$infile: $ +!";
        would be a conventional way of doing that. Notice also that read returns the number of bytes read, which you can use to check that you get back the number you expect - if data can be wrong it will be!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://846472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-02-05 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?