Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a binary file into an array

Tags:

file-io

vb6

What's the fastest way (using VB6) to read an entire, large, binary file into an array?

like image 875
Dave Avatar asked Mar 16 '10 16:03

Dave


People also ask

How do I read a binary file in Python?

To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable.


2 Answers

Here's one way, although you are limited to files around 2 GB in size.

  Dim fileNum As Integer
  Dim bytes() As Byte

  fileNum = FreeFile
  Open "C:\test.bin" For Binary As fileNum
  ReDim bytes(LOF(fileNum) - 1)
  Get fileNum, , bytes
  Close fileNum
like image 105
raven Avatar answered Oct 02 '22 09:10

raven


You can compare these two

Private Function ReadFile1(sFile As String) As Byte()
    Dim nFile       As Integer

    nFile = FreeFile
    Open sFile For Input Access Read As #nFile
    If LOF(nFile) > 0 Then
        ReadFile1 = InputB(LOF(nFile), nFile)
    End If
    Close #nFile
End Function

Private Function ReadFile2(sFile As String) As Byte()
    Dim nFile       As Integer

    nFile = FreeFile
    Open sFile For Binary Access Read As #nFile
    If LOF(nFile) > 0 Then
        ReDim ReadFile2(0 To LOF(nFile) - 1)
        Get nFile, , ReadFile2
    End If
    Close #nFile
End Function

I prefer the second one but it has this nasty side effect. If sFile does not exists For Binary mode creates an empty file no matter that Access Read is used.

like image 38
wqw Avatar answered Oct 05 '22 09:10

wqw