Results 1 to 3 of 3

Thread: vb6 binary file help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    7

    Unhappy vb6 binary file help

    Hi

    can someone explain to me how i solve this problem:
    1. I need to copy first half of FileA to FileB (first half=7.600.000 bytes)
    2. copy/print a string to FileB
    3. Copy last half of FileA to FileB

    I know how to "print" the string in nr. 2. , so thats not the problem (Using "Put")

    Can you also show me an example??

    Thanks!

    \\Batnas
    Last edited by Batnas; Jan 26th, 2009 at 04:18 PM.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    7

    Re: vb6 binary file help

    p.s. Im using VB6

  3. #3
    Addicted Member
    Join Date
    Dec 2008
    Posts
    129

    Re: vb6 binary file help

    So basically what you are trying to do is insert a string inside of a file. I don't know how to copy parts of a file like you asked, but I do have some code that might be useful. You can copy the whole FileA to FileB and then use this code to insert the string.

    Code:
    Public Function InsertData(ByVal FileName As String, DataToInsert() As Byte, ByVal InsertPos As Long)    '
    '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    '  Original thread: http://www.vbforums.com/showthread.php?t=433537
    '
    Const cBuffSize As Long = 262144 ' 256 KBytes
    Dim Buffer() As Byte, BuffPos As Long
    
    'open the file
    Open FileName For Binary As #1
    
    ' Shift all data to the right
    If LOF(1) - InsertPos < cBuffSize Then
            ' we can do it in one copy, we don't need a loop
            ' resize the buffer so we don't copy too much (more than file size)
            ReDim Buffer(LOF(1) - InsertPos)  '<=  Removed: - 1 to avoid losing the last byte!
            ' copy and paste the data to the new location
            Get 1, InsertPos, Buffer
            Put 1, InsertPos + UBound(DataToInsert()) + 1, Buffer
    Else
            ' we start from the end of the file
            BuffPos = LOF(1) + 1 '<= Added: MUST add 1 to avoid losing the last byte!
            ' we HAVE to shift from right to left,
            ' otherwise we override important data
    
            Do Until BuffPos <= InsertPos
                'we HAVE to dimension this now or the Buffer will
                'remain 256K in length for the last read,
                'overwriting data we previously moved
                'it is Redimmed later if we need the full Buffer size
                ReDim Buffer(BuffPos - InsertPos - 1)         '<= Added
                ' substract the buffer size from current position
                BuffPos = BuffPos - cBuffSize
                If BuffPos < InsertPos Then
                    ' we reached the last copy
                    ' data passed the InsertPos position, so we have to
                    ' resize the buffer so it does not go over InsertPos                                                ReDim Buffer(cBuffSize - (InsertPos - BuffPos) - 1)
                    BuffPos = InsertPos
                Else
                    ReDim Buffer(cBuffSize - 1)
                End If
                            ' copy and paste the data to the new location
                Get 1, BuffPos, Buffer
                Put 1, BuffPos + 1 + UBound(DataToInsert()), Buffer
            Loop
    End If
    ' Insert the actual data
    Put 1, InsertPos, DataToInsert
    
    'close the file
    Close #1
    End Function

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