Coder's Guild Mailing List

perl code problem #2

Posted by Frank Hale on 2001-01-13

Okay forget the first question I sent out. That code was foobared. Take this
code example which reads in a text file named test.txt. It gets its size and
then reads 1 byte at a time placing each byte into an array. Then it
attempts to print each byte in the byte array as a hex number. Except that
it prints all zero's instead of printing the hex value for each byte. What
am I doing wrong?

BEGIN {

 my @file_bytes;

 # open a file for reading
 open(INFILE, "test.txt") or die("Error: unable to open file");

 $file_size = (stat("test.txt"))[7];

 for ($a = 0; $a < $file_size; $a++)
 {
  seek (INFILE, $a, 0);
  sysread(INFILE, $byte, 1);
  push @file_bytes,$byte;

  $byte="";
 }


 foreach $byte (@file_bytes)
 {
  printf("%2.2x ", $byte);
 }

}

--
ICQ: 7205161
http://sapphire.sourceforge.net/ - A small window manager for X11
http://javathingies.sourceforge.net/ - My java junk

"The box said it requires Windows 95 or better so I installed Linux"