Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^4: Problem on substr record on binary file

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

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

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); }

Replies are listed 'Best First'.
Re^5: Problem on substr record on binary file
by cdarke (Prior) on Jun 25, 2010 at 09:27 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2023-01-29 19:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found