我知道如何在VB.Net中这样做但在vb6中不是一个想法 我要做的是避免阅读整个文件 这可能吗?
答案 0 :(得分:1)
您可以使用随机访问打开文件。一次向后移动一个字节,计算回车换行字符对的数量。将每一行存储在一个数组或类似的东西中,当你读完400行时,停止。
答案 1 :(得分:1)
Cometbill有一个很好的答案。
打开随机访问文件:
Open filename For Random Access Read As #filenumber Len = reclength
以字节为单位获取文件的长度:
FileLen(ByVal PathName As String) As Long
从随机访问文件中读取:
Get [#]filenumber,<[recnumber]>,<varname>
重要提示: <varname>
函数中的Get
必须是固定长度的字符串Dim varname as String * 1
,否则如果Bad record length (Error 59)
错误,则会{1}}变量被声明为可变长度字符串,如Dim varname as String
只是想指出在Dim varname as String * 1
中你定义了一个固定长度的字符串,长度是1.这是你希望使用read-1-byte-backwardwards方法。如果你的文件有固定长度的记录,一次不需要1个字节,你可以一次读取一个记录(不要忘记为回车和换行添加2个字节)。在后一种情况下,您将定义Dim varname as String * X
,其中X是记录长度+ 2.然后一个简单的循环向后移动400次或直到达到文件的开头。
答案 2 :(得分: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