Results 1 to 15 of 15

Thread: Inserting/Deleting bytes in a binary file.

  1. #1

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628

    Resolved 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.

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Inserting/Deleting bytes in a binary file.

    Vanguard-MnC,

    What would be the reasoning behind something like this?

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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).

  5. #5

    Thread Starter
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628

    Re: Inserting/Deleting bytes in a binary file.

    Okay, I got it. Thanks!

  6. #6
    New Member
    Join Date
    Dec 2005
    Posts
    1

    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

  7. #7
    Lively Member Ansoft's Avatar
    Join Date
    Jul 2005
    Posts
    76

    Re: Inserting/Deleting bytes in a binary file.

    Yes, I am also being in interest to see the Code
    Ansoft The code mad

    VB Forum is my master

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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.

  9. #9
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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.
    Last edited by DigiRev; Oct 17th, 2006 at 06:02 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Dim File As String
    2. Dim Insert As String
    3. Dim InsertPos As Long
    4.  
    5. File = "ABCDEFGHIJ" ' our file
    6.  
    7. Insert = "+---+" ' what you want to insert
    8. InsertPos = 3    ' insert at position
    9.  
    10. File = File & Space(Len(Insert)) ' make file Len(Insert) bigger
    11.  
    12. '  Shift the data to the right by Len(Insert) bytes
    13. Mid$(File, InsertPos + Len(Insert)) = Mid$(File, InsertPos, Len(File) - InsertPos - Len(Insert) + 1)
    14.  
    15. ' insert (copy) the actual data
    16. Mid$(File, InsertPos, Len(Insert)) = Insert
    17.  
    18. 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.

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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...

  12. #12
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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:
    1. Dim File As String
    2. Dim Insert As String
    3. Dim InsertPos As Long
    4.  
    5. File = "ABCDEFGHIJ" ' our file
    6.  
    7. Insert = "+---+" ' what you want to insert
    8. InsertPos = 3    ' insert at position
    9.  
    10. File = File & Space(Len(Insert)) ' make file Len(Insert) bigger
    11.  
    12. '  Shift the data to the right by Len(Insert) bytes
    13. Mid$(File, InsertPos + Len(Insert)) = Mid$(File, InsertPos, Len(File) - InsertPos - Len(Insert) + 1)
    14.  
    15. ' insert (copy) the actual data
    16. Mid$(File, InsertPos, Len(Insert)) = Insert
    17.  
    18. 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...

  13. #13
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

    Visual Studio 6, Visual Studio.NET 2005, MASM

  14. #14
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Open "C:\Test.txt" For Append Access Write As #1
    2.     Seek #1, 2
    3.     Print #1, "test"
    4.     Close #1
    The data in the file was "0123456789", and after the insert it ended up with:
    Code:
    0test
    789
    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.

  15. #15
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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
  •  



Click Here to Expand Forum to Full Width