celcia
celcia

Reputation:

How can I process a binary file of fixed length records with no line breaks in between?

I have a text file that's composed of fixed length records but all in one line with no line breaks in between. What's the best way to process it in Perl? Thanks!

Upvotes: 1

Views: 1510

Answers (3)

Brian Agnew
Brian Agnew

Reputation: 272347

unpack() may be of use here. You can specify the list of characters (using 'c', 'C' or 'W') and it'll unpack automatically into a list. See the pack documentation for the options to use.

Upvotes: 2

user80168
user80168

Reputation:

First, let's open the file, and make sure it's in bin mode:

open my $fh, '<', 'file.name' or die "Cannot open file.name: $!";
binmode $fh;

Now, set input record separator to reference to length of your records (let's assume 120 bytes per record):

local $/ = \120;

Now, let's read the records:

while (my $record = <$fh>) {

And now if you want to get data out of it, you have to write some unpack thing:

  my @elements = unpack("......", $record);

Now you can process @elements, and finish while() {} loop:

  ...
}

Whole "program":

open my $fh, '<', 'file.name' or die "Cannot open file.name: $!";
binmode $fh;
local $/ = \120;
while (my $record = <$fh>) {
  my @elements = unpack("......", $record);
  ...
}
close $fh;

Upvotes: 8

hillu
hillu

Reputation: 9611

use the read FILEHANDLE,SCALAR,LENGTH function to read a block at a time into a buffer...

use constant LEN => 60;
while (!eof $fh) {
    my $len = read $fh, $buf, LEN;
    die "short read" if $len < LEN;
    # processing...
}

... and process the buffer using regular expressions, unpack, or however you like.

Upvotes: 5

Related Questions