Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb6 我想从txt文件中读取最后400行_Vb6 - Fatal编程技术网

Vb6 我想从txt文件中读取最后400行

Vb6 我想从txt文件中读取最后400行,vb6,Vb6,我知道如何在VB.Net中实现这一点,但在vb6中没有任何想法。 我要做的是避免读取整个文件。 可能吗?您可以使用随机访问打开文件。一次后退一个字节,计算回车换行符对的数量。将每一行存储在一个数组或类似的东西中,当你读完400行后,停下来。Cometbill有一个很好的答案 要打开文件进行随机访问,请执行以下操作: Open filename For Random Access Read As #filenumber Len = reclength 要获取以字节为单位的文件长度,请执行以下操作

我知道如何在VB.Net中实现这一点,但在vb6中没有任何想法。
我要做的是避免读取整个文件。

可能吗?

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

Cometbill有一个很好的答案

要打开文件进行随机访问,请执行以下操作:

Open filename For Random Access Read As #filenumber Len = reclength
要获取以字节为单位的文件长度,请执行以下操作:

FileLen(ByVal PathName As String) As Long
要从随机访问文件中读取,请执行以下操作:

Get [#]filenumber,<[recnumber]>,<varname>
Get[#]filenumber,,
重要提示:来自
Get
函数的
必须是一个固定长度的字符串
Dim varname as string*1
,否则,如果变量像这样声明为变长字符串
Dim varname as string
,它将出错为
错误记录长度(错误59)

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

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

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

首先,您将如何在VB.NET中实现它?(显然,如果不读取整个文件或让其他人为您读取文件,就无法计算文件中的换行。)文件句柄中应该有一个名为
seek
的东西。检查一下。哈布夫,当你说“避免读取整个文件”时,你的意思是不将整个文件读取到内存中,而是一次读取一行整个文件可以吗?因为这里的一些答案探索了整个文件,一次读一行。我不确定你是否能接受。请澄清。