Overwrite just one line? Am I a n00b or is this impossible? Both? :D

M

Matt@revera

I'd like to overwrite just one line of a binary file, based on a
position set by seek(). Is there no way to do this? As far as I can
tell I need to read the whole file, change the line, and write it all
back out. Not exactly easy on the memory, but I see no other solution.

so far:

patchme.seek(offset)
patchme.write(a2b_hex(edit)) # the data is in hex first
patchme.close
print "Patching complete"

This writes the data at the given offset, but _everything_ before it is
filled with 0's.

Sorry for the potentially n00b question, I'm stumped!
 
E

Erik Max Francis

Matt@revera said:
I'd like to overwrite just one line of a binary file, based on a
position set by seek(). Is there no way to do this? As far as I can
tell I need to read the whole file, change the line, and write it all
back out. Not exactly easy on the memory, but I see no other solution.

You need to find what "just one line" means in a binary format. If the
chunk you're replacing and the chunk you want to replace it with are of
different sizes, then you need to use a temporary file (or read the
remainder of the file in memory).

Otherwise, open the file in read-write binary mode ("r+b") and seek and
write appropriately. In the general case, you need to write to a
temporary file to get the job done.

Memory usage is not a factor, here; read and write the temporary files
in chunks. That way you can manage the memory usage to whatever upper
bound you wish.
 
M

Matt@revera

Erik said:
solution.

You need to find what "just one line" means in a binary format. If the
chunk you're replacing and the chunk you want to replace it with are of
different sizes, then you need to use a temporary file (or read the
remainder of the file in memory).

Otherwise, open the file in read-write binary mode ("r+b") and seek and
write appropriately. In the general case, you need to write to a
temporary file to get the job done.

Memory usage is not a factor, here; read and write the temporary files
in chunks. That way you can manage the memory usage to whatever upper
bound you wish.

--
Erik Max Francis && (e-mail address removed) && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Be able to be alone. Lose not the advantage of solitude.
-- Sir Thomas Browne

Ok makes sense, I was using a+b. Would r+b be a better choice? I was
under the impression that it was only for reading. I am writing
something of the same length in fact, but the python reference says
that on some versions of unix (I'm on mac OS X) append will ignore the
seek() position and dump it at the bottom of the file - which is
exactly what it's doing. Am I stuck?

Thanks for all your help!
 
M

Matt@revera

Ok the key was "r+b" as opposed to "a+b" but why is that? R is for
read, correct? And b for binary. Adding the plus gives me some form
of write capability?
 
E

Erik Max Francis

Matt@revera said:
Ok the key was "r+b" as opposed to "a+b" but why is that? R is for
read, correct? And b for binary. Adding the plus gives me some form
of write capability?

Read binary is "rb," but read-write binary is "r+b." The "+" means that
you can write, too.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top