in reply to Re^3: How to read number of block in binary files?
in thread How to read number of block in binary files?

Hi.. Thank for your info.. that very helpful.. But could you guide me how could i read the binary files until end of file ?..

This is my latest code:-

if ($fname) { open (OUTPUT, ">$fname.tmp") or die "Can't open $fname.tmp $!\ +n"; open (DATA, "<$fname") or die "Can't open $fname $!\n"; binmode DATA; read(DATA, $data, BLOCKHDR); $blockhdr = unpack "H*", $data; read(DATA, $data, BLOCKDATA); $blockdt = unpack "H*", $data; my $t_cdr = hextoint(reverse_str(substr($blockdt,4))); my $blocklen = $t_cdr * 286; read(DATA, $data, $blocklen); my $record = unpack "H*", $data; close(DATA); close(OUTPUT); } else {

Replies are listed 'Best First'.
Re^5: How to read number of block in binary files?
by jwkrahn (Abbot) on Mar 10, 2009 at 10:24 UTC

    This is UNTESTED but you may want code something like this:

    Update to while loop conditional.

    use constant HEADER_LEN => 32; use constant RECORD_LEN => 286; use constant DUMMY => "\0" x RECORD_LEN; if ( $fname ) { open my $OUTPUT, '>:raw', "$fname.tmp" or die "Cannot open '$fname +.tmp' $!"; open my $INPUT, '<:raw', $fname or die "Cannot open '$fname +' $!"; RECORD: { my ( $offset, $bytes_read, $header ) = ( 0, 0, '' ); # while ( $bytes_read < HEADER_LEN ) { while ( $offset < HEADER_LEN ) { defined( $bytes_read = read $INPUT, $header, HEADER_LEN - +$offset, $offset ) or die "Cannot read from '$fname' $!"; $bytes_read or last RECORD; # EOF $offset += $bytes_read; } # format may be either 'x30 n' or 'x30 S' depending on endiane +ss? my $total_record = unpack 'x30 n', $header; my $records_len = $total_record * RECORD_LEN; ( $offset, $bytes_read, my $data ) = ( 0, 0, '' ); # while ( $bytes_read < $records_len ) { while ( $offset < $records_len ) { defined( $bytes_read = read $INPUT, $data, $records_len - +$offset, $offset ) or die "Cannot read from '$fname' $!"; $bytes_read or last RECORD; # EOF $offset += $bytes_read; } $header = pack 'a30 n', $header, 3; print $OUTPUT $header, $data, $total_record == 2 ? DUMMY : ''; redo; } }