Problem with Reading a Binary File

T

tony.fountaine

I am relatively new to using perl and have run into a problem reading a
binary file. It seems like it only occurs when reading a short with
value of 26 or 0x001a. I decide to create a simple program to
reproduce the problem and succeded. Could someone tell me what is
wrong with the following code.


$outfile='>c:\data.txt';
$infile='c:\data.txt';

open(OUT, $outfile) or die "Could not open output file";
$b = pack("S", 26);
print OUT "$b";
close (OUT) ;

open(IN, $infile) or die "Could not open input file";
sysread (IN, $a,2) ;
$a = unpack("S", $a);
close(IN);

print "Value of a = $a";

Thanks
Tony
 
J

John Bokma

I am relatively new to using perl and have run into a problem reading a
binary file. It seems like it only occurs when reading a short with
value of 26 or 0x001a.

Yup, which in DOS speak means end of file:

perldoc -f binmode
 
U

usenet

I decide to create a simple program to
reproduce the problem

When I run your program (on UNIX), I get:

Value of a = 26

which is what I expect to see. What are you getting (and what did you
expect to see?)
 
T

tony.fountaine

I was expecting to get the value 26 as you did. However, I got
nothing. After reviewing the other replies and adding binmode(IN) and
binmode(OUT) to the script I succeeding in reading the value 26.
Thanks for all your help.
 
J

John Bokma

I was expecting to get the value 26 as you did. However, I got
nothing. After reviewing the other replies and adding binmode(IN) and
binmode(OUT) to the script I succeeding in reading the value 26.
Thanks for all your help.

You might want to have a look at File::Slurp if you want to slurp a binary
file, and write it back.
 
S

Sherm Pendley

$outfile='>c:\data.txt';
$infile='c:\data.txt';

open(OUT, $outfile) or die "Could not open output file";

Some folks have already helped you with your main question - but here's
another suggestion I hope you'll find useful: Include the filename and $!
in the error message printed by die(), like this:

open(OUT, $outfile) or die "Could not open $outfile for writing: $!";

Including the filename lets you verify that $outfile is what you think
it should be, and $! will tell you *why* the file couldn't be opened.

sherm--
 
U

usenet

I decide to create a simple program to
reproduce the problem

When I run your program (on UNIX), I get:

Value of a = 26

which is what I expect to see. What are you getting (and what did you
expect to see?)
 
T

tony.fountaine

I was expecting to get the value 26 as you did. However I consistantly
got nothing. After reviewing the other replies and adding binmode(IN)
and binmode(OUT) to the script I succeeding in reading the value 26.
Thanks for all your help.
 
T

tony.fountaine

I was expecting to get the value 26 like you did but I got nothing.
However after review the messages above and adding the line binmode(IN)
and binmode(OUT) to the corresponding files it fixed the problem.
Thanks everyone for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top