#! c:/perl/bin/perl; use strict; use warnings; #======================= # Define VARS # ====================== my $RECORD_SIZE = 40; my $ID = 0; my $size = 20; # Num of Bytes to read from fi +le my $offset = 0; # Offset to file reading. my $MASK = 'H4C3H4b12H4'; my $file = "FileName"; open( FILE, $file ) || die print "Can not open file"; # open file binmode FILE; # Turn on Binary mode # Read Header for the record. while ( sysread( FILE, $data, $size) ){ #Filehandle,str +ing,length,offset my ($flag,$format,$blocks,$source,$no,$time,$type) = unpack $MASK, + $data; print "Header: $flag\n"; print "Header Data:",unpack $MASK, $data; print "\n"; # Check integity of the header. if ( $flag != 1234 ) { die print "Ill formed header. File may be corrupt."; # Read the remainder of the record. $size = ( ( $RECORD_SIZE * $blocks ) - 20 ); sysread( FILE, $data, $size ); #Filehandle,string,length,offset # Debugging stuff to match Hex editor to file to determine what is goi +ng on. my $long = length($data); print "Size of Data portion of record: $long \n"; my $temp = "H" . $long; #print $temp; my $alldata = unpack $temp, $data; print "Data: $alldata\n"; } close FILE;
In reply to Re: Reading Binary file confusion
by Anonymous Monk
in thread Reading Binary file confusion
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |