469,912 Members | 1,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,912 developers. It's quick & easy.

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

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!

Jul 18 '05 #1
1 1642
Matt@revera wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && 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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Perrin | last post: by
5 posts views Thread by MattG | last post: by
2 posts views Thread by Rich | last post: by
9 posts views Thread by martinezfive | last post: by
2 posts views Thread by hzgt9b | last post: by
2 posts views Thread by Eric B. | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.