Reading a binary file into an array

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

+7
file-io vb6
source share
2 answers

Here is one way, although you are limited to files about 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 
+8
source share

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, but it has this unpleasant side effect. If sFile does not exist, For Binary mode creates an empty file regardless of what Access Read using.

+5
source share

All Articles