如何知道一个文件有多少行?

fernery 2004-03-16 05:08:23
i=0
Do While Not EOF(1)
Line Input #1, str1
i=i+1
loop
close #1
这个方法可行,但是太慢有无其他方法可以快速知道文件有多少?
...全文
115 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hanan 2004-03-17
  • 打赏
  • 举报
回复
先将文件读如一个缓存,strBuffer

然后:

  intLines = Len(strBuffer) - Len(Replace(strBuffer, vbCr, "")) + 1

flyingZFX 2004-03-17
  • 打赏
  • 举报
回复
太好了,先UP一下
reminious 2004-03-17
  • 打赏
  • 举报
回复
'注释 file.path=app.path
'对当前某个文件的行数的读取计算
Dim hT1 As Integer, hCount As Integer, hFreeNumber As Integer
Dim tmp As String
tmp = ""
hCount = 0
For hT1 = 0 To File.ListCount - 1
If File.Selected(hT1) = True Then
hFreeNumber = FreeFile
If Len(dir.Path) > 3 Then
Open dir.Path + "\" + File.List(hT1) For Input As #hFreeNumber
Else
Open dir.Path + File.List(hT1) For Input As #hFreeNumber
End If

While Not EOF(hFreeNumber)
Line Input #hFreeNumber, tmp
hCount = hCount + 1
If tmp = "" Then hCount = hCount - 1
Wend

Close #hFreeNumber
End If
Next
MsgBox "计算完毕!"
labTot.Caption = "档案总共行数" + Chr$(13) + Chr$(10) + Str$(hCount)
vikingleo 2004-03-17
  • 打赏
  • 举报
回复
mark
mmcgzs 2004-03-17
  • 打赏
  • 举报
回复
高手如云
KiteGirl 2004-03-16
  • 打赏
  • 举报
回复
以下是测试数据:

tText = TextGetByFile("SY2.txt")
Text1.Text = TextLineCountGet(tText)

编译前:1.375秒
编译后:0.328125(简易版)

tText = TextGetByFile("SY2.txt")
Text1.Text = UBound(Split(tText, Chr(13) & Chr(10)))

编译前:0.1640625
编译后:0.171875

由此可见,用Split函数是最快的办法。
KiteGirl 2004-03-16
  • 打赏
  • 举报
回复
测试语句:(SY2.txt为金庸《神雕侠侣》全文,1863K,8493行。编译后速度很快。)
tText = TextGetByFile("SY2.txt") '从文件获得文本
Text1.Text = TextLineCountGet(tText) '测试文本的行数
Text2.Text = TextLineCountGet_ForView(tText, Text2) '测试文本的行数,并在工作的同时于Text2显示百分比进度。


Function TextLineCountGet(ByVal pText As String) As Long
'简易版
Dim tOutLng As Long
Dim tBytes() As Byte
Dim tIndex As Long
Dim tIndexOver As Long

tBytes() = StrConv(pText, vbFromUnicode)
tIndexOver = UBound(tBytes)

For tIndex = 0 To tIndexOver
tOutLng = tOutLng + ((tBytes(tIndex) = 13) And 1)
'以上相当于语句:If tBytes(tIndex) = 13 Then tOutLng = tOutLng + 1
Next

TextLineCountGet = tOutLng
End Function

Function TextLineCountGet_ForView(ByVal pText As String, Optional pViewTextBox As TextBox) As Long
'进度指示版。可完全按照TextLineCountGet来用,当你指定pViewTextBox后,会在该TextBox显示进度。
Dim tOutLng As Long
Dim tBytes() As Byte
Dim tIndex As Long
Dim tIndexOver As Long
Dim tEventStep As Long

tOutLng = (Len(pText) > 1) And 1

If CBool(tOutLng) Then

tBytes() = StrConv(pText, vbFromUnicode)
tIndexOver = UBound(tBytes)

tEventStep = (tIndexOver \ 100) + ((tIndexOver < 100) And 1)
'显示刷新步长计算:本算法可防止tEventStep=0

For tIndex = 0 To tIndexOver
tOutLng = tOutLng + ((tBytes(tIndex) = 13) And 1)
'以上相当于语句:If tBytes(tIndex) = 13 Then tOutLng = tOutLng + 1

If Not CBool(tIndex Mod tEventStep) Then
If Not pViewTextBox Is Nothing Then
pViewTextBox.Text = tIndex * 100 \ tIndexOver
DoEvents
End If
End If
Next

End If

TextLineCountGet_ForView = tOutLng
End Function

Function TextGetByFile(ByVal pFileName As String) As String
'从文件获得多行文本。
Dim tOutText As String
Dim tFileNumber As Integer
Dim tBytes() As Byte
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber
If CBool(LOF(tFileNumber)) Then
ReDim tBytes(LOF(tFileNumber) - 1)
Get tFileNumber, 1, tBytes()
tOutText = StrConv(tBytes, vbUnicode)
Else
MsgBox "警告:文件" & pFileName & "是空的!", vbOKOnly, "文件无内容"
End If
Close #tFileNumber
TextGetByFile = tOutText
End Function
KiteGirl 2004-03-16
  • 打赏
  • 举报
回复
还可以这样:
Private Sub Command2_Click()
Dim tBytes() As Byte
Dim tFileSize As Long
Dim tIndex As Long
Dim tIndexOver As Long
Dim tLineCount As Long
Open "SY.txt" For Binary As #1
tFileSize = LOF(1) - 1
ReDim tBytes(tFileSize)
tIndexOver = tFileSize
Get #1, 1, tBytes()
Close #1
For tIndex = 0 To tIndexOver
tLineCount = tLineCount + ((tBytes(tIndex) = 13) And 1)
Next
Text1.Text = tLineCount
End Sub
稍后给你函数。
pigpag 2004-03-16
  • 打赏
  • 举报
回复
应该不会。

一般,做成普通文件和数据库,两者速度上有本质区别。否则无论用什么手段,速度基本上不会有本质提高。因为其本质是要找一个文件中有多少换行符Chr(10)
TIANHEI 2004-03-16
  • 打赏
  • 举报
回复
用filesystemobject会不会快些?
yoki 2004-03-16
  • 打赏
  • 举报
回复
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;Persist Security Info=False;Mode=Share Deny None"
rs.Open "select 1 from [test#txt]", cn
Debug.Print "文本c:\test.txt共有行数为:" & rs.RecordCount
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
rainstormmaster 2004-03-16
  • 打赏
  • 举报
回复
如果文件特别大的话,建议以数据库方式进行操作,这样速度会有根本性的提高
rainstormmaster 2004-03-16
  • 打赏
  • 举报
回复
dim mfile as string
mfile="c:\test.txt"
dim buff() as byte
dim i as long
i=filelen(mfile)
redim buff(i-1)
open mfile for binary as #1
get #1,,buff
close #1
dim s as string
s=strconv(buff,vbunicode)
erase buff
dim arr() as string
arr=split(s,vbcrlf)
msgbox "文件共有行数:"+cstr(ubound(arr)+1)

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧