Recipe 8.14 Reading a String from a Binary File

8.14.1 Problem

You want to read a NUL-terminated string from a file, starting at a particular address.

8.14.2 Solution

Ensure you're working with a binary file, set $/ to an ASCII NUL, and read the string with <>:

binmode(FH);                        # binary mode $old_rs = $/;                       # save old $/ $/ = "\0";                          # ASCII 0: NUL seek(FH, $addr, SEEK_SET)           or die "Seek error: $!\n"; $string = <FH>;                     # read string chomp $string;                      # remove NUL $/ = $old_rs;                       # restore old $/

You can use local to save and restore $/:

{     local $/ = "\0";     # ... }                           # $/ is automatically restored

8.14.3 Discussion

The example program shown in Example 8-5, bgets, accepts a filename and one or more byte addresses as arguments. Decimal, octal, or hexadecimal addresses may be specified. For each address, the program reads and prints the null- or EOF-terminated string at that position.

Example 8-5. bgets
  #!/usr/bin/perl -w   # bgets - get a string from an address in a binary file   use IO::Seekable;   use open IO => ":raw";              # binary mode on all opened handles   ($file, @addrs) = @ARGV             or die "usage: $0 file addr ...";   open(FH, $file)                     or die "cannot open $file: $!";   $/ = "\000";   foreach $addr (@addrs) {       $addr = oct $addr if $addr =~ /^0/;       seek(FH, $addr, SEEK_SET)           or die "can't seek to $addr in $file: $!";       printf qq{%#x %#o %d "%s"\n}, $addr, $addr, $addr, scalar <>;   }

Example 8-6 is a simple implementation of the Unix strings program.

Example 8-6. strings
  #!/usr/bin/perl -w   # strings - pull strings out of a binary file   $/ = "\0";   use open IO => ":raw";   while (<>) {       while (/([\040-\176\s]{4,})/g) {           print $1, "\n";       }   }

8.14.4 See Also

The PerlIO(3) manpage; the seek, getc, and ord functions in perlfunc(1) and in Chapter 29 of Programming Perl; the discussion of qq// in the "Quote and Quote-Like Operators" section of the perlop(1) manpage, and in the "Pick Your Own Quotes" section of Chapter 2 of Programming Perl



Perl Cookbook
Perl Cookbook, Second Edition
ISBN: 0596003137
EAN: 2147483647
Year: 2003
Pages: 501

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net