将二进制文件读入数组

时间:2010-03-16 16:22:24

标签: file-io vb6

使用VB6将整个大型二进制文件读入数组的最快方法是什么?

2 个答案:

答案 0 :(得分:8)

这是一种方法,但您只能使用大小为2 GB的文件。

  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

答案 1 :(得分:5)

你可以比较这两个

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

我更喜欢第二种,但它有这种令人讨厌的副作用。如果sFile不存在,For Binary模式会创建一个空文件,无论使用Access Read