vba 读取大文本文件 VB6 中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13598691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read Number of lines in Large Text File VB6
提问by chotai.mit
I have text File of Size 230MB. I want to Count Number of Lines OF that File.
我有大小为 230MB 的文本文件。我想计算该文件的行数。
I tried "Scripting.FileSystemOblect
" but it goes out Of memory.
我试过“ Scripting.FileSystemOblect
”,但它超出了内存。
Please Help.
请帮忙。
Thanks.
谢谢。
回答by Bob77
Normal Windows line breaks are CRLF, so you can count the LFs and add 1 to the count in cases where the last line of your files doesn't have one after it.
正常的 Windows 换行符是 CRLF,因此您可以计算 LF,并在文件的最后一行后面没有的情况下将计数加 1。
In true VB (i.e. VB5, VB6, etc.) you can make use of the byte-oriented String operations to speed many tasks. If we can assume the text files contain ANSI then this is pretty fast:
在真正的 VB(即 VB5、VB6 等)中,您可以利用面向字节的 String 操作来加速许多任务。如果我们可以假设文本文件包含 ANSI,那么这将非常快:
Option Explicit
Private Sub Main()
Const BUFSIZE As Long = 100000
Dim T0 As Single
Dim LfAnsi As String
Dim F As Integer
Dim FileBytes As Long
Dim BytesLeft As Long
Dim Buffer() As Byte
Dim strBuffer As String
Dim BufPos As Long
Dim LineCount As Long
T0 = Timer()
LfAnsi = StrConv(vbLf, vbFromUnicode)
F = FreeFile(0)
Open "big.txt" For Binary Access Read As #F
FileBytes = LOF(F)
ReDim Buffer(BUFSIZE - 1)
BytesLeft = FileBytes
Do Until BytesLeft = 0
If BufPos = 0 Then
If BytesLeft < BUFSIZE Then ReDim Buffer(BytesLeft - 1)
Get #F, , Buffer
strBuffer = Buffer 'Binary copy of bytes.
BytesLeft = BytesLeft - LenB(strBuffer)
BufPos = 1
End If
Do Until BufPos = 0
BufPos = InStrB(BufPos, strBuffer, LfAnsi)
If BufPos > 0 Then
LineCount = LineCount + 1
BufPos = BufPos + 1
End If
Loop
Loop
Close #F
'Add 1 to LineCount if last line of your files do not
'have a trailing CrLf.
MsgBox "Counted " & Format$(LineCount, "#,##0") & " lines in" & vbNewLine _
& Format$(FileBytes, "#,##0") & " bytes of text." & vbNewLine _
& Format$(Timer() - T0, "0.0#") & " seconds."
End Sub
Given a 7,000,000 line file of 293MB it only takes 0.7 seconds here. But note that I had not rebooted to ensure that the file wasn't cached when I ran that test. Without caching (i.e. after a reboot) I'd expect it to take as long as 5 times that.
给定一个 293MB 的 7,000,000 行文件,这里只需要 0.7 秒。但请注意,当我运行该测试时,我没有重新启动以确保文件没有被缓存。如果没有缓存(即重新启动后),我希望它需要 5 倍的时间。
Converting to handle Unicode text files is fairly simple. Just replace the B-functions by the non-B equivalents, make sure you set BUFSIZE to a multiple of 2, and search for vbLf
instead of an ANSI LF byte.
转换为处理 Unicode 文本文件相当简单。只需用非 B 等效项替换 B 函数,确保将 BUFSIZE 设置为 2 的倍数,然后搜索vbLf
而不是 ANSI LF 字节。
回答by xpda
You can do it by reading each line into the same variable. There's no need to save all the lines:
您可以通过将每一行读入同一个变量来实现。无需保存所有行:
dim s as string
dim n as integer
open "filename.txt" for input as 1
n = 0
do while not eof(1)
line input #1, s
n = n + 1
loop
This has not been tested, and it's been a while since I've done any VB6, but it should be close.
这还没有经过测试,自从我做过任何 VB6 以来已经有一段时间了,但它应该很接近。
回答by Alex K.
This takes about 6 seconds for me on a 480mb binary file with 1mil+ 0xD (vbcr)
在 1mil+ 0xD (vbcr) 的 480mb 二进制文件上,这对我来说大约需要 6 秒
Dim buff() As Byte
Dim hF As Integer
Dim i As Long, n As Long
hF = FreeFile(0)
Open "c:\windows\Installerf91fd.msp" For Binary Access Read As #hF
ReDim buff(LOF(hF) - 1)
Get #hF, , buff()
Close #hF
For i = 0 To UBound(buff)
If buff(i) = 13 Then n = n + 1
Next
MsgBox n