SPUG: Not reading to end of binary file on the PC

Ken McGlothlen mcglk at artlogix.com
Mon May 7 16:10:57 CDT 2001


"Cheryl Tornquist (Seattle)" <ctornquist at dotcast.com> writes:

| I have a chance to prove the usefulness of PERL in my new company, but I'm
| stumped on a problem and hope someone else may know the answer.  [...]  I'm
| already using binmode.
| 
| The script reads a proprietary binary file and gets different results between
| a sun/solaris environment (PERL ver. 5.6.0) and Windows 2000 environment
| (ActiveState build 522 and build 623).

Well, that's the first problem.  Windows has traditionally used ^Z for EOF.  I
suspect that that bit of cruft is still true in Win2K, and it might not be
handled well by ActiveState Perl.  (I don't know.  I don't code for Windows
anymore.)

| The code works perfectly on the Unix box (reads in all the file and formats
| it to a text file) but only part of the binary log file is read when using
| Win2000 (the required OS).  A section of relevant pseudo code is below.  When
| running on the PC, the script reads the 12 fixed bytes until the last line
| which returns only 11 bytes.  This is when it encounts the hex code 001a 4e00
| for the first time on all three sample files.

Yep.  \x1A is ^Z.

Now, I don't have a Windows machine here to test this out on, but I suspect you
need sysopen() and sysread() for this.  Instead of:

| #!/usr/bin/perl
| open (LOGENTRY, "< $binlog") or die "Can't open file $binlog -- $!.\n";
| binmode (LOGENTRY);
| $template = "H8 B32 B16 B16";
| undef $/;
| while (read LOGENTRY, $logentry, 12) {   # read in the whole file and
| process
|    unpack
|    read the rest of the entry
|    write to text file
| }
| close LOGENTRY;

you'd have to have something like this:

	#!/usr/bin/perl

	sysopen( LOGENTRY, $binlog, 0 );
	my $template = "H8 B32 B16 B16";
	while( sysread( LOGENTRY, $logentry, 12 ) ) {
		blah, blah, blah;
	}
	close( LOGENTRY );

This *should* work, but I can't guarantee it without a Windows box here to test
it out on.  Best of luck, though.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list