Reading & Writing Binary files

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Reading & Writing Binary files

  1. #1
    Join Date
    Aug 2005

    Reading & Writing Binary files


    I am attempting to read a binary file and then write the contents to a new file. However for some reason some extra characters are placed at the top of the destination file apart from that the files are identical.

    Here is the code:

    Option Explicit
    Private Sub Form_Load()
        Call writeBinFile(App.Path & "\", readBinFile(App.Path & "\"))
    End Sub
    Function writeBinFile(ByVal bfilename As String, data As Variant)
        Dim FileNum As Long
        FileNum = FreeFile
        Open bfilename For Binary Access Write As #FileNum
        Put #FileNum, , data
        Close #FileNum
    End Function
    Function readBinFile(ByVal bfilename As String) As Variant
        Dim fl As Long
        Dim FileNum As Long
        Dim binbyte() As Byte
        Dim binfilestr As String
        On Error GoTo errHandler
        FileNum = FreeFile
        Open bfilename For Binary Access Read As #FileNum
        fl = FileLen(bfilename)
        ReDim binbyte(fl)
        Get #FileNum, , binbyte
        Close #FileNum
        readBinFile = binbyte
        Exit Function
              Exit Function
    End Function
    I don’t believe the readBinFile function is causing the problem since its documented on Microsoft’s website;en-us;193998

    *confused* the attached pictures shows the unknown data appended at the beginning of the file.

    PS: The solution can not be achieved by simply copying the file.
    Attached Images Attached Images

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    Try changing writeBinFile to this:
    Sub writeBinFile(ByVal bfilename As String, Data() As Byte)
        Dim FileNum As Long
        FileNum = FreeFile
        Open bfilename For Binary Access Write As #FileNum
        Put #FileNum, , Data
        Close #FileNum
    End Sub
    You may also want to add code to delete the destination file if it already exists.
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Aug 2005
    Thanks very much Phil! works a charm still very unusual behaviour of a variant.

Similar Threads

  1. Binary files
    By jojo in forum VB Classic
    Replies: 1
    Last Post: 08-21-2005, 09:50 PM
  2. Replies: 7
    Last Post: 06-23-2005, 11:39 PM
  3. Reading Large Binary Files for String Operations
    By Ryan Shelley in forum Java
    Replies: 0
    Last Post: 01-16-2002, 06:57 PM
  4. Reading binary data from SQL Server database
    By Danny Desmet in forum VB Classic
    Replies: 2
    Last Post: 09-22-2000, 03:15 AM
  5. Reading binary file
    By Russ McTyre in forum Java
    Replies: 1
    Last Post: 06-13-2000, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center