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

In reply to How to read a binary file with file header, data and file trailer ? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.