Tuesday, April 24, 2007

My idea was to access binary data using PERL. Specifically, if the data is of variable length.
For example, I have a data stream: 1 12 123 12 34. With PERL, can I write this data to a file? The answer is sure, I can. Actually writing the above variable length data is easy. All you have to do is open the file in binary mode and dump the data.

But, retrieving the data is an issue. I am not able to get hold of it...

Here is the code:

#! /usr/bin/perl

# to read and store binary files
# ref: --> http://www.herongyang.com/perl/binary.html

# gives problem with variable length data..

# part 1) to write binary data to a file
# this works correct.

@floatvalues = (23.3, 3.2, 4.3, 5.4);
$out = "test.raw";
open(OUT, "> $out");
binmode(OUT);
foreach $value (@floatvalues) {
print OUT "$value\n";
print "$value ";
}
print "\n";
close(OUT);

# part 2) to read binary data from a file
# reading is a trouble. As read function has a parameter identifying the data
# width.

$IN = "test.raw";
open(IN, "$IN");

while (read(IN, $value, 3)) {
print "$value \n";
}

close(IN);


How to overcome this?

No comments: