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
    Posts
    2

    Reading & Writing Binary files

    Hi,

    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:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
        Call writeBinFile(App.Path & "\copy.zip", readBinFile(App.Path & "\original.zip"))
    
    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
    
    errHandler:
              Exit Function
    End Function
    I donít believe the readBinFile function is causing the problem since its documented on Microsoftís website http://support.microsoft.com/default...b;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
    Location
    Portland, OR
    Posts
    8,387
    Try changing writeBinFile to this:
    Code:
    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
    http://www.philweber.com

    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
    Posts
    2
    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, 08:50 PM
  2. Replies: 7
    Last Post: 06-23-2005, 10:39 PM
  3. Reading Large Binary Files for String Operations
    By Ryan Shelley in forum Java
    Replies: 0
    Last Post: 01-16-2002, 05:57 PM
  4. Reading binary data from SQL Server database
    By Danny Desmet in forum VB Classic
    Replies: 2
    Last Post: 09-22-2000, 02:15 AM
  5. Reading binary file
    By Russ McTyre in forum Java
    Replies: 1
    Last Post: 06-13-2000, 11:14 AM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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