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?
Re: Inserting/Deleting bytes in a binary file.
Vanguard-MnC,
What would be the reasoning behind something like this?
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.
Re: Inserting/Deleting bytes in a binary file.
Quote:
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).
Re: Inserting/Deleting bytes in a binary file.
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
Re: Inserting/Deleting bytes in a binary file.
Yes, I am also being in interest to see the Code
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.
Re: Inserting/Deleting bytes in a binary file.
Quote:
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.
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.
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...
Re: Inserting/Deleting bytes in a binary file.
Quote:
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...
Re: Inserting/Deleting bytes in a binary file.
Quote:
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
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
Quote:
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.
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