bh_perl has asked for the wisdom of the Perl Monks concerning the following question:
Hi..
I want to read a binary file and the binary files have File Header, data and File Trailer. File Header, File Trailer and Data is fix length.
But I have a problem to capture the File Trailer. Could somebody help me How could i read the files by following the file format ?
#!/usr/bin/perl -w use POSIX qw(strftime); use constant FILEHDR => 66; use constant DATALEN => 222; use constant FILETRL => 69; ... ... sub print_header { my $data = shift; my $loc = 0; my $nloc = 0; my $filehdr = unpack "A*", $data; my $fileval = unpack "H*", $data; print ("FILE HEADER\n"); for ($i = 0; $i < @hdrname; $i++) { my $len = $hdrsize[$i]; if ($hdrname[$i] =~ /newLine/) { printf (" %-20s : %-15s >%s<\n", $hdrname[$i +], substr($fileval,$nloc,$len*2), substr($fileval, $nloc, $len*2)); } else { printf (" %-20s : %-15s >%s<\n", $hdrname[$i +], substr($filehdr, $loc, $len), substr($fileval, $nloc, $len*2)); } $loc = $loc + $len; $nloc = $nloc + $len * 2; } } sub print_trailer { my $data = shift; my $loc = 0; my $nloc = 0; my $trlhdr = unpack "A*", $data; my $trlval = unpack "H*", $data; print ("FILE HEADER\n"); for ($i = 0; $i < @trlname; $i++) { my $len = $trlsize[$i]; if ($trlname[$i] =~ /newLine/) { printf (" %-20s : %-15s >%s<\n", $trlname[$i +], substr($trlval,$nloc,$len*2), substr($trlval, $nloc, $len*2)); } else { printf (" %-20s : %-15s >%s<\n", $trlname[$i +], substr($trlval, $loc, $len), substr($trlval, $nloc, $len*2)); } $loc = $loc + $len; $nloc = $nloc + $len * 2; } } sub display_data { my $data = shift; my $val; my $tmp; my $cdr; my $loc = 0; print ("\nDATA\n") if ($raw); for ($i = 0; $i < @dataname; $i++) { my $len = $datasize[$i]; my $fname = $dataname[$i]; if (uc($fname) =~ /RECORD TYPE|CANCEL TYPE/) { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } elsif (uc($fname) =~ /RECORD NUMBER/) { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } else { $cdr = hextoasc (substr($data, $loc, $len)); $val = substr($data, $loc, $len); } $cdr =~ s/\s+//g; $val =~ s/\s+//g; if ($raw) { printf (" %-20s: %-25s >%s<\n", $fname, $cdr +, $val); } elsif ($std) { printf (" %-20s: %-25s\n", $fname, $cdr); } else { if ($output) { printf (OUTPUT "%s,", $cdr); } else { printf ("%s,", $cdr); } } $loc = $loc + $len; if ($output) { print (OUTPUT "\n"); } else { print "\n"; } } if ($fname) { if (!defined ($input)) { $input = getcwd; } if (defined ($output)) { open (OUTPUT, ">$output/$fname.csv"); } open (DATA, "$input/$fname"); binmode DATA; read (DATA, $data, FILEHDR); print_header($data) if ($raw); while (read (DATA, $data, DATALEN) != 0) { my $hex = unpack "H*", $data; $rtype = hextoasc (substr($hex, 0, 2)); if ($rtype =~ /T/) { print_trailer($hex) if ($raw); } else { display_data($hex); } } close(DATA); close(OUTPUT) if ($output); } else { usage(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to read a binary file with file header, data and file trailer ?
by BrowserUk (Patriarch) on Feb 03, 2010 at 09:39 UTC | |
by bh_perl (Monk) on Feb 03, 2010 at 10:17 UTC | |
by BrowserUk (Patriarch) on Feb 03, 2010 at 11:28 UTC | |
by bh_perl (Monk) on Feb 04, 2010 at 03:35 UTC | |
|
Re: How to read a binary file with file header, data and file trailer ?
by Anonymous Monk on Feb 03, 2010 at 09:11 UTC | |
by bh_perl (Monk) on Feb 03, 2010 at 09:36 UTC |