|
-
Jul 21st, 2005, 03:10 PM
#1
Thread Starter
Fanatic Member
Inserting/Deleting bytes in a binary file.
I need to know how to insert some data, let's say about 30 - 40 bytes, into a file at a certain address without losing any data from the file, and I also need to know how to delete bytes at a certain range (not just overwrite it with a bunch of 00's), for example $4000 - $4FFF. How would I go about doing this?
Last edited by Vanguard-MnC; Jul 21st, 2005 at 08:46 PM.
-
Jul 21st, 2005, 05:56 PM
#2
Re: Inserting/Deleting bytes in a binary file.
Vanguard-MnC,
What would be the reasoning behind something like this?
-
Jul 21st, 2005, 06:19 PM
#3
Frenzied Member
Re: Inserting/Deleting bytes in a binary file.
You'll need to load the entire file into memory, delete the file on disk, make the changes and make a new file on disk.
-
Jul 21st, 2005, 07:20 PM
#4
Re: Inserting/Deleting bytes in a binary file.
 Originally Posted by jeroen79
You'll need to load the entire file into memory, delete the file on disk, make the changes and make a new file on disk.
yes, that would be the easiest way... but what if you have a file that's 10 GBytes ? (that was just an example of course).
For large files to load in the memory, make modifications and write back on disk.... i don't think so...
The proper way to do this is to "shift" the data...
When you insert data, then you have to shift to the right, then insert the data.
When you delete the data, then you have to shift to the left, and delete the trailing bytes (there's an API to truncate a file).
-
Jul 21st, 2005, 08:46 PM
#5
Thread Starter
Fanatic Member
Re: Inserting/Deleting bytes in a binary file.
-
Oct 7th, 2006, 02:18 AM
#6
New Member
Re: Inserting/Deleting bytes in a binary file.
Vanguard,
Good that you found the solution. But, Please post the solution here. so that it helps newbie like me. 
Satya
-
Oct 17th, 2006, 11:35 AM
#7
Lively Member
Re: Inserting/Deleting bytes in a binary file.
Yes, I am also being in interest to see the Code
-
Oct 17th, 2006, 02:32 PM
#8
Re: Inserting/Deleting bytes in a binary file.
I'll write some code and post it in the CodeBank, but right now I don't have time cuz I'm at work. Hopefully I'll have time later when I get home.
If I don't reply, remind me again in a few days.
-
Oct 17th, 2006, 05:50 PM
#9
Re: Inserting/Deleting bytes in a binary file.
 Originally Posted by CVMichael
I'll write some code and post it in the CodeBank, but right now I don't have time cuz I'm at work. Hopefully I'll have time later when I get home.
If I don't reply, remind me again in a few days.
I hope so, because I have been wanting to know how to do this as well...that is, insert data into a binary file, and push the already existing data forward without overwriting it...
Is this done with API? If so, could you at least post the API function(s) used for this?
Edit: I've found that the SetEndOfFile() API can be used to cut off the end of a file at a certain point, but as far as shifting data, I don't know what function to use.
Last edited by DigiRev; Oct 17th, 2006 at 06:02 PM.
-
Oct 17th, 2006, 08:46 PM
#10
Re: Inserting/Deleting bytes in a binary file.
I don't know of any API to do this, but it is quite easy to do it "manually"...
This is an example using strings how to insert data, and same theory you use for files:
VB Code:
Dim File As String
Dim Insert As String
Dim InsertPos As Long
File = "ABCDEFGHIJ" ' our file
Insert = "+---+" ' what you want to insert
InsertPos = 3 ' insert at position
File = File & Space(Len(Insert)) ' make file Len(Insert) bigger
' Shift the data to the right by Len(Insert) bytes
Mid$(File, InsertPos + Len(Insert)) = Mid$(File, InsertPos, Len(File) - InsertPos - Len(Insert) + 1)
' insert (copy) the actual data
Mid$(File, InsertPos, Len(Insert)) = Insert
Debug.Print File
I will start working on a example with files, i'm not sure if i'll have time to finish today though... I'll have to do testing, that's gonna take longer
Believe it or not, I never actually have done this, never needed to do it, but i'm sure it will work.
-
Oct 17th, 2006, 09:50 PM
#11
Re: Inserting/Deleting bytes in a binary file.
Here's the thread in the CodeBank:
http://www.vbforums.com/showthread.php?t=433537
I will post the Deletion part tomorow, cuz now I have to go to sleep, I have a long day tomorow...
-
Oct 17th, 2006, 10:33 PM
#12
Re: Inserting/Deleting bytes in a binary file.
 Originally Posted by CVMichael
I don't know of any API to do this, but it is quite easy to do it "manually"...
This is an example using strings how to insert data, and same theory you use for files:
VB Code:
Dim File As String
Dim Insert As String
Dim InsertPos As Long
File = "ABCDEFGHIJ" ' our file
Insert = "+---+" ' what you want to insert
InsertPos = 3 ' insert at position
File = File & Space(Len(Insert)) ' make file Len(Insert) bigger
' Shift the data to the right by Len(Insert) bytes
Mid$(File, InsertPos + Len(Insert)) = Mid$(File, InsertPos, Len(File) - InsertPos - Len(Insert) + 1)
' insert (copy) the actual data
Mid$(File, InsertPos, Len(Insert)) = Insert
Debug.Print File
I will start working on a example with files, i'm not sure if i'll have time to finish today though... I'll have to do testing, that's gonna take longer
Believe it or not, I never actually have done this, never needed to do it, but i'm sure it will work.
Yea that's pretty easy, but the problem is that won't work with binary files, and it won't work with large files very well because you'd have to load the entire file into memory...
There is a Seek method in VB that I think will work, ie:
Open File For Binary Access Write As #1
Seek #1, 3
Put #1, , "Data..."
Close #1
That will write "Data..." starting at the 3rd byte, but I think it overwrites data that was already there, I don't think it shifts it forward...
-
Oct 17th, 2006, 10:41 PM
#13
Re: Inserting/Deleting bytes in a binary file.
 Originally Posted by DigiRev
Yea that's pretty easy, but the problem is that won't work with binary files, and it won't work with large files very well because you'd have to load the entire file into memory...
There is a Seek method in VB that I think will work, ie:
Open File For Binary Access Write As #1
Seek #1, 3
Put #1, , "Data..."
Close #1
That will write "Data..." starting at the 3rd byte, but I think it overwrites data that was already there, I don't think it shifts it forward...
I'm pretty sure that if you open for append, using seek will allow you to "insert" it without overwriting.
Don't quote me on that though 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 18th, 2006, 07:21 AM
#14
Re: Inserting/Deleting bytes in a binary file.
DigiRev, I DID post the solution in the CodeBank in post #12, here's the link again:
http://www.vbforums.com/showthread.php?t=433537
 Originally Posted by chemicalNova
I'm pretty sure that if you open for append, using seek will allow you to "insert" it without overwriting.
Don't quote me on that though
chem
No, it won't work, I did a quick test:
VB Code:
Open "C:\Test.txt" For Append Access Write As #1
Seek #1, 2
Print #1, "test"
Close #1
The data in the file was "0123456789", and after the insert it ended up with:
And the file size increased only by 2 bytes (probably the new line after the "test")
As you can see, it still overrided the data, instead of inserting, test it for yourself, and you will see...
Again.... I DID post the solution in the CodeBank, just take a look, and you will see that you need to shift the data before inserting the data.
Last edited by CVMichael; Oct 18th, 2006 at 07:26 AM.
-
Oct 19th, 2006, 10:05 PM
#15
Re: Inserting/Deleting bytes in a binary file.
If you guys are still interested, I just updated the thread in the CodeBank to delete data from a file.
http://www.vbforums.com/showthread.php?t=433537
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|