CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How do I randomly update a binary file?

 



Jasmine
Administrator

Jan 26, 2001, 10:26 AM

Post #1 of 1 (1779 views)
How do I randomly update a binary file? Can't Post

(From the Perl FAQ)

How do I randomly update a binary file?

If you're just trying to patch a binary, in many cases something as simple as this works:

Code
    perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs

However, if you have fixed sized records, then you might do something more like this:

Code
    $RECSIZE = 220; # size of record, in bytes 
$recno = 37; # which record to update
open(FH, "+<somewhere") || die "can't update somewhere: $!";
seek(FH, $recno * $RECSIZE, 0);
read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
# munge the record
seek(FH, $recno * $RECSIZE, 0);
print FH $record;
close FH;

Locking and error checking are left as an exercise for the reader. Don't forget them, or you'll be quite sorry.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives