473,467 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1765
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gord | last post by:
Hello, If you set the flag for an overwrite prompt using the 'Save' common dialog, how do you read the response when the user clicks the Yes or No in the 'overwrite' message box? Everything...
2
by: Perrin | last post by:
Hi, I am new to these forums so I don't know if this is the appropriate place to post it, but Iam really stuck and I need help :( I have this assignment with structs in C++. basically the...
5
by: MattG | last post by:
Can someone point me towards something explaining how to overwrite an exe that is currently being executed, if even possible? I'm trying to build in an update routine to ahve it look for a newer...
2
by: Rich | last post by:
Hello, I need to write to the console window, but I don't want to add a new line, and I don't want to write the next value adjacent to the previous value. I just want to overwrite the previous...
9
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
1
by: Aere | last post by:
Hello, I'm coding a program that saves information about my patients, so i created a rich textbox for every field. (Name, Tel, etc). The program saves a .txt for each field, no matter if there is...
2
by: hzgt9b | last post by:
I know how to overwrite a function. Normally this is what I would do: function someFunction() { /* orig definition here */ } //later in the execution stream I would do... someFunction = function...
8
by: Joe Duchtel | last post by:
Hello - I have the following code to detemine a file name when my application is saving a file. The problem is that if the file already exists and I select the Yes button in the "Do you want to...
2
by: Eric B. | last post by:
I coulda sworn it was possible to overwrite specific lines in a text file without disturbing the rest, or was that in C++? Will a TextWriter object do this? Eric B.
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.