473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

overwrite binary data

I've written a program in VB.net. I need to open a file in binary mode and
edit it. I cannot find a way to partially overwrite existing data. There
are certain hex values I need to replace in the file. I am looking at the
System.IO.BinaryWriter class. There only seems to be a way to append or
completely truncate the contents of a file. Does anybody have an idea how I
might accomplish this??

Thanks,
Christine
Jul 19 '05 #1
5 5554
I should state that I actually want to delete parts of the binary data also
(but not all of it).

-Christine

"Christine Nguyen" <hc********@hotmail.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
I've written a program in VB.net. I need to open a file in binary mode and edit it. I cannot find a way to partially overwrite existing data. There
are certain hex values I need to replace in the file. I am looking at the
System.IO.BinaryWriter class. There only seems to be a way to append or
completely truncate the contents of a file. Does anybody have an idea how I might accomplish this??

Thanks,
Christine

Jul 19 '05 #2

"Christine Nguyen" <hc********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I should state that I actually want to delete parts of the binary data also
(but not all of it).

-Christine

"Christine Nguyen" <hc********@hotmail.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
I've written a program in VB.net. I need to open a file in binary mode

and
edit it. I cannot find a way to partially overwrite existing data. There
are certain hex values I need to replace in the file. I am looking at the
System.IO.BinaryWriter class. There only seems to be a way to append or
completely truncate the contents of a file. Does anybody have an idea how

I
might accomplish this??

Thanks,
Christine


I don't think you can edit a file directly on the disk.

You have to load the file into an array or other structure using a streamreader,
then operate on it, and then overwrite the file using a streamwriter.
Jul 19 '05 #3
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP***********************@news.microsoft.com. ..
Chris Devol <xy*@defghijk.lmn> wrote:
I don't think you can edit a file directly on the disk.


Yes you can - there's no problem doing that. You can't insert or delete
bits from the start of middle of the file, of course, but that's a
different matter.


It seems to me that inserting and deleting is what "editing" is. So if you can't
insert/delete then you can't edit. Or am I missing something?
You have to load the file into an array or other structure using a
streamreader, then operate on it, and then overwrite the file using
a streamwriter.


Note that StreamReader and StreamWriter are meant for text operations,
not the binary operations Christine is interested in.


That's true. I should have said "BinaryReader"..........
Jul 19 '05 #4
Chris Devol <xy*@defghijk.lmn> wrote:
Chris Devol <xy*@defghijk.lmn> wrote:
I don't think you can edit a file directly on the disk.


Yes you can - there's no problem doing that. You can't insert or delete
bits from the start of middle of the file, of course, but that's a
different matter.


It seems to me that inserting and deleting is what "editing" is. So
if you can't insert/delete then you can't edit. Or am I missing something?


Yes. You can overwrite. Consider a file which has fixed length records
- it's perfectly possible to overwrite those records with new ones.
Alternatively, you might have something which goes through an assembly
marking all public methods as internal, or vice versa - something which
just requires changing a single byte for each method. No need to read
the whole file in and write it all out again.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #5
Hi,

I accomplished using the FileStream Class. It will do binary operations and
insert without copying to a temp file and will delete at the tail end of the
file using Filestream.SetLength.

Thanks,
Christine
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:Xn*****************@207.46.248.16...
"Christine Nguyen" <hc********@hotmail.com> wrote in
news:e7**************@TK2MSFTNGP10.phx.gbl:
I've written a program in VB.net. I need to open a file in binary
mode and edit it. I cannot find a way to partially overwrite existing
data. There are certain hex values I need to replace in the file. I
am looking at the System.IO.BinaryWriter class. There only seems to
be a way to append or completely truncate the contents of a file. Does
anybody have an idea how I might accomplish this??
The BinaryWriter class has a seek method which you can use to position the
file pointer at any point and overwrite the data at that position.

As far as inserting, you will have to use a temp file and read all the

data up to the insertion point, write that data to the temp file, then write the data you want to insert to the temp file, and finally read the remaining
data and write it to the temp file. Assuming all has gone well, delete the original file and rename the temp file.

A similar procedure must be used to delete information from a file.

Chris
--
If you don't like lunchmeat, please remove it from my e-mail address to
send me an e-mail

Jul 19 '05 #6

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: Florian Preknya | last post by:
Is there a posibility to overwrite a private method (one that starts with __ ) ? I read that they are just formely private, they are prefixed with the class name to have an obtured visibility, so...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
1
by: Matt | last post by:
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...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
2
by: Lisa Pearlson | last post by:
Hi, My php application (on Apache/Linux) needs to do the following: The PHP script receives a request from a client (binary), asking for certain records of data. My PHP script loops through...
1
by: Someonekicked | last post by:
I have a binary file, and I need to delete a specific number of characters in it. The file contains records, each record has a specific length. So the way I wanna handle deleting is that I will...
2
by: B-Dog | last post by:
Is there a way to make vb.net to overwrite the file when moving? Here is what I'm trying to do: If System.IO.File.Exists(dest) Then 'handle overwrite here If MessageBox.Show("Do you want...
6
by: Christine Nguyen | last post by:
I've written a program in VB.net. I need to open a file in binary mode and edit it. I cannot find a way to partially overwrite existing data. There are certain hex values I need to replace in the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.