1

我知道如何在 VB.Net 中做到这一点,但在 vb6 中不知道。
我要实现的是避免读取整个文件。
那可能吗?

4

3 回答 3

1

您可以使用随机访问打开文件。一次向后工作一个字节,计算回车换行字符对的数量。将每一行存储在一个数组或类似的东西中,当你读完 400 行时,停止。

于 2012-10-29T15:10:44.740 回答
1

Cometbill 有一个很好的答案。

要打开文件以进行随机访问:

Open filename For Random Access Read As #filenumber Len = reclength

要以字节为单位获取文件的长度:

FileLen(ByVal PathName As String) As Long

从随机访问文件中读取:

Get [#]filenumber,<[recnumber]>,<varname>

重要提示:函数的<varname>fromGet必须是固定长度的字符串Dim varname as String * 1,否则Bad record length (Error 59)如果将变量声明为像这样的可变长度字符串,则会出错Dim varname as String

编辑:

只是想指出,Dim varname as String * 1您正在定义一个固定长度的字符串,长度为 1。这是如果您希望使用 read-1-byte-backwards 方法。如果您的文件有固定长度的记录,则无需一次走 1 个字节,您可以一次读取一条记录(不要忘记添加 2 个字节用于回车和换行)。在后一种情况下,您将定义Dim varname as String * XX 是记录长度 + 2 的位置。然后一个简单的循环向后 400 次或直到到达文件的开头。

于 2012-10-29T15:41:07.503 回答
0

以下是我对此的看法。如果您有一个非常大的文件,这比前两个答案更有效,因为我们不必将整个文件存储在内存中。

Option Explicit

Private Sub Command_Click()

    Dim asLines()                           As String

    asLines() = LoadLastLinesInFile("C:\Program Files (x86)\VMware\VMware Workstation\open_source_licenses.txt", 400)

End Sub

Private Function LoadLastLinesInFile(ByRef the_sFileName As String, ByVal the_nLineCount As Long) As String()

    Dim nFileNo                             As Integer
    Dim asLines()                           As String
    Dim asLinesCopy()                       As String
    Dim bBufferWrapped                      As Boolean
    Dim nLineNo                             As Long
    Dim nLastLineNo                         As Long
    Dim nNewLineNo                          As Long
    Dim nErrNumber                          As Long
    Dim sErrSource                          As String
    Dim sErrDescription                     As String

    On Error GoTo ErrorHandler

    nFileNo = FreeFile

    Open the_sFileName For Input As #nFileNo

    On Error GoTo ErrorHandler_FileOpened

    ' Size our buffer to the number of specified lines.
    ReDim asLines(0 To the_nLineCount - 1)
    nLineNo = 0

    ' Read all lines until the end of the file.
    Do Until EOF(nFileNo)
        Line Input #nFileNo, asLines(nLineNo)
        nLineNo = nLineNo + 1
        ' Check to see whether we have got to the end of the string array.
        If nLineNo = the_nLineCount Then
            ' In which case, flag that we did so, and wrap back to the beginning.
            bBufferWrapped = True
            nLineNo = 0
        End If
    Loop

    Close nFileNo

    On Error GoTo ErrorHandler

    ' Were there more lines than we had array space?
    If bBufferWrapped Then
        ' Create a new string array, and copy the bottom section of the previous array into it, followed
        ' by the top of the previous array.
        ReDim asLinesCopy(0 To the_nLineCount - 1)
        nLastLineNo = nLineNo
        nNewLineNo = 0
        For nLineNo = nLastLineNo + 1 To the_nLineCount - 1
            asLinesCopy(nNewLineNo) = asLines(nLineNo)
            nNewLineNo = nNewLineNo + 1
        Next nLineNo
        For nLineNo = 0 To nLastLineNo
            asLinesCopy(nNewLineNo) = asLines(nLineNo)
            nNewLineNo = nNewLineNo + 1
        Next nLineNo
        ' Return the new array.
        LoadLastLinesInFile = asLinesCopy()
    Else
        ' Simply resize down the array, and return it.
        ReDim Preserve asLines(0 To nLineNo)
        LoadLastLinesInFile = asLines()
    End If

Exit Function

ErrorHandler_FileOpened:
    ' If an error occurred whilst reading the file, we must ensure that the file is closed
    ' before reraising the error. We have to backup and restore the error object.
    nErrNumber = Err.Number
    sErrSource = Err.Source
    sErrDescription = Err.Description
    Close #nFileNo
    Err.Raise nErrNumber, sErrSource, sErrDescription

ErrorHandler:
    Err.Raise Err.Number, Err.Source, Err.Description
End Function
于 2012-10-30T09:28:04.930 回答