in reply to reading binary files with Perl
If the number of fields is wrong subtitute unpack 'V' for unpack 'N'. If the float is wrong try the reverseed value that is commented out.#!/usr/bin/perl -w use strict; open FILE, 'file.bin' or die "Couldn't open file: $!\n"; binmode FILE; my $record = 1; my $buffer = ''; while ( read( FILE, $buffer, 4 ) ) { my $record_length = unpack 'N', $buffer; my $num_fields = $record_length / 4; printf "Record %d. Number of fields = %d\n", $record, $num_fie +lds; for (1 .. $num_fields ) { read( FILE, $buffer, 4 ); my $temperature = unpack 'f', $buffer; # Or if the above gives the wrong result try this: #my $temperature = unpack 'f', reverse $buffer; print "\t", $temperature, "\n"; } # Read but ignore record trailer. read( FILE, $buffer, 4 ); print "\n"; $record++; } __END__
Update: Added read for trailer.
--
John.
|
|---|