读取大文本文件 VB6 中的行数 [英] Read Number of lines in Large Text File VB6

查看:49
本文介绍了读取大文本文件 VB6 中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大小为 230MB 的文本文件.我想计算该文件的行数.

I have text File of Size 230MB. I want to Count Number of Lines OF that File.

我尝试了Scripting.FileSystemOblect",但内存不足.

I tried "Scripting.FileSystemOblect" but it goes out Of memory.

请帮忙.

谢谢.

推荐答案

正常的 Windows 换行符是 CRLF,因此您可以计算 LFs 并在文件的最后一行没有的情况下将计数加 1之后.

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.

在真正的 VB(即 VB5、VB6 等)中,您可以利用面向字节的字符串操作来加速许多任务.如果我们可以假设文本文件包含 ANSI,那么这将非常快:

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:

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

给定一个 293MB 的 7,000,000 行文件,这里只需要 0.7 秒.但请注意,当我运行该测试时,我没有重新启动以确保文件没有被缓存.如果没有缓存(即重新启动后),我希望它需要 5 倍的时间.

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.

转换为处理 Unicode 文本文件相当简单.只需将 B 函数替换为非 B 等效项,确保将 BUFSIZE 设置为 2 的倍数,然后搜索 vbLf 而不是 ANSI LF 字节.

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.

这篇关于读取大文本文件 VB6 中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆