Option Explicit '强制宣告定义变量 Dim AppDisk$, S '定义变量 Private Sub Form_Load() '将窗体居中显示 (屏幕宽度减去窗体的宽度)除以2 , (屏幕高度减去窗体的高度)除以2 Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 '将本地路径赋值给变量 AppDisk AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\") End SubPrivate Sub Command1_Click() Open AppDisk & "test.txt" For Input As #1 Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode) Close #1 S = Split(Text1.Text, vbNewLine) MsgBox "总共有:" & CStr(UBound(S) + 1) & " 行" End Sub
txt文件总行数?用line读出到末尾得出的行数行不?
给你另一种较"高级"的方法Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const EM_GETLINECOUNT = &HBA Dim AppDisk$ Private Sub Form_Load() AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\") Open AppDisk & "test.txt" For Input As #1 Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode) Close #1 MsgBox "本文总共有 " & CStr(GetTLineCnt(Text1)) & " 行" End SubPublic Function GetTLineCnt(TxBox As Object) As Long GetTLineCnt = SendMessage(TxBox.hwnd, EM_GETLINECOUNT, 0, 0) End Function
Dim TextLine as string dim l as long Open "TESTFILE" For Input As #1 ' 打开文件。 Do While Not EOF(1) ' 循环至文件尾。 Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。 l=l+1 Loop Close #1 ' 关闭文件。 debug.print "文件有" & l-1 & "行。"
Private Sub Command1_Click() Dim i As Long Open App.Path & "\test.txt" For Input As #1 Do Until EOF(1) i = i + 1 Loop Close #1 MsgBox "总共有:" & i + 1 & " 行" End Sub
Private Sub Command1_Click() Dim i As Long Open App.Path & "\test.txt" For Input As #1 Do Until EOF(1) Line Input #1, nextline i = i + 1 Loop Close #1 MsgBox "总共有:" & i & " 行" End Sub
讲到效率, 1F 的代码也不该直接给控件 Text1, 而是要先给变量 处理完后再赋值给Text1Option Explicit '强制宣告定义变量 Dim AppDisk$, aa$, S '定义变量 Private Sub Form_Load() '将窗体居中显示 (屏幕宽度减去窗体的宽度)除以2 , (屏幕高度减去窗体的高度)除以2 Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 '将本地路径赋值给变量 AppDisk AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\") End SubPrivate Sub Command1_Click() Open AppDisk & "test.txt" For Input As #1 aa = StrConv(InputB(LOF(1), 1), vbUnicode) Close #1 S = Split(aa, vbNewLine) Text1.Text = aa MsgBox "总共有:" & CStr(UBound(S) + 1) & " 行" End Sub
第一种情况:文件不是很大。Dim tBytes() as Byte Dim tFN As Integer Dim tText As String Dim tLines() As String'把文本文件整个读入内存 tFN = FreeFile Open FileName For Binary As #tFN ReDim tBytes(LOF(tFN)-1) Get #tFN, 1, tBytes() Close #FNtText = Strconv(tText, vbUnicode)'以换行符分割为字符串数组。每一个元素是一行。 tLines() = Split(tText, vbCrLf)'根据数组的元素数量取得行数。 tLinesCount = UBound(tLines()) + 1 第二种情况:文件非常大。 (这个用到分页缓冲,所以用大概的语言描述它。这种方法比上面那个还要快。)<打开文件> <以一定的容量分页读取文件字节,通常缓冲区大于64KB> <历遍页面,查找换行符,找到则计数器加1> <关闭文件> <计数器就是行数>在我的博客里有个“巨文本索引器”,那玩意儿就兼顾有取得特别大文本文件行数的功能。 但无论如何,由于变量限制,文本文件不能大于2GB。
Dim ff As Integer, sLineFromFile As String, i As Long i = 0 c = 0 If ExistFile(sFile$) = False Then MsgBox "调取数据时出错,请检查文件是否存在!", vbCritical, "出错" Close ff Exit Function End If ff = FreeFile Open sFile$ For Input Access Read As ff Do While Not EOF(ff) DoEvents Line Input #ff, sLineFromFile$ If Len(sLineFromFile$) > 2 Then c = c + 1 '得到文件行数 Loop Close ff
用line三.取出最后的一行的数就行了. fno=freefile() 'fno是文件号 do until eof(fno) open "你的文本文档",for input as #fno line input #fno,newline loop print newline 'newline是最后一行的行数.就等于一共有多少行了三. close #fno
Dim AppDisk$, S '定义变量
Private Sub Form_Load()
'将窗体居中显示 (屏幕宽度减去窗体的宽度)除以2 , (屏幕高度减去窗体的高度)除以2
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
'将本地路径赋值给变量 AppDisk
AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
End SubPrivate Sub Command1_Click()
Open AppDisk & "test.txt" For Input As #1
Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
S = Split(Text1.Text, vbNewLine)
MsgBox "总共有:" & CStr(UBound(S) + 1) & " 行"
End Sub
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETLINECOUNT = &HBA
Dim AppDisk$
Private Sub Form_Load()
AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
Open AppDisk & "test.txt" For Input As #1
Text1.Text = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
MsgBox "本文总共有 " & CStr(GetTLineCnt(Text1)) & " 行"
End SubPublic Function GetTLineCnt(TxBox As Object) As Long
GetTLineCnt = SendMessage(TxBox.hwnd, EM_GETLINECOUNT, 0, 0)
End Function
dim l as long
Open "TESTFILE" For Input As #1 ' 打开文件。
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
l=l+1
Loop
Close #1 ' 关闭文件。
debug.print "文件有" & l-1 & "行。"
Dim i As Long
Open App.Path & "\test.txt" For Input As #1
Do Until EOF(1)
i = i + 1
Loop
Close #1
MsgBox "总共有:" & i + 1 & " 行"
End Sub
Dim i As Long
Open App.Path & "\test.txt" For Input As #1
Do Until EOF(1)
Line Input #1, nextline
i = i + 1
Loop
Close #1
MsgBox "总共有:" & i & " 行"
End Sub
Dim AppDisk$, aa$, S '定义变量
Private Sub Form_Load()
'将窗体居中显示 (屏幕宽度减去窗体的宽度)除以2 , (屏幕高度减去窗体的高度)除以2
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
'将本地路径赋值给变量 AppDisk
AppDisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
End SubPrivate Sub Command1_Click()
Open AppDisk & "test.txt" For Input As #1
aa = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
S = Split(aa, vbNewLine)
Text1.Text = aa
MsgBox "总共有:" & CStr(UBound(S) + 1) & " 行"
End Sub
一般情况下也不会有这么变态的文件吧。在 VB 中,大文件可以 Binary模式 分块读取,搜索统计换行符的个数。
不清楚“内存映射”方式速度如何…………
结合split函数,直接就可以得到。
假设已经将整个文本读到S里,那
msgbox “一共” & ubound(split(s,vbcrlf)) & "行"
不过没人会要求我处理速度一定要和3W的一样快 那不现实
片面追求效率一次性读入全部数据在实际开发中并不可取
因为谁都不知道以后会不会遇到3G甚至10G以上的数据文件
如果文件超大 一次性读入的后果是什么?
这就是处理外来数据的难处了
所以如果数据是自己生成的 一定要使用定长的数据记录格式
否则后患无穷
估计ms的系统分析员也没敢不管文件大小一次性全部读入
Dim tFN As Integer
Dim tText As String
Dim tLines() As String'把文本文件整个读入内存
tFN = FreeFile
Open FileName For Binary As #tFN
ReDim tBytes(LOF(tFN)-1)
Get #tFN, 1, tBytes()
Close #FNtText = Strconv(tText, vbUnicode)'以换行符分割为字符串数组。每一个元素是一行。
tLines() = Split(tText, vbCrLf)'根据数组的元素数量取得行数。
tLinesCount = UBound(tLines()) + 1 第二种情况:文件非常大。
(这个用到分页缓冲,所以用大概的语言描述它。这种方法比上面那个还要快。)<打开文件>
<以一定的容量分页读取文件字节,通常缓冲区大于64KB>
<历遍页面,查找换行符,找到则计数器加1>
<关闭文件>
<计数器就是行数>在我的博客里有个“巨文本索引器”,那玩意儿就兼顾有取得特别大文本文件行数的功能。
但无论如何,由于变量限制,文本文件不能大于2GB。
然后然后用Split分隔获取总行数.
i = 0
c = 0
If ExistFile(sFile$) = False Then
MsgBox "调取数据时出错,请检查文件是否存在!", vbCritical, "出错"
Close ff
Exit Function
End If
ff = FreeFile
Open sFile$ For Input Access Read As ff
Do While Not EOF(ff)
DoEvents
Line Input #ff, sLineFromFile$
If Len(sLineFromFile$) > 2 Then c = c + 1 '得到文件行数
Loop
Close ff
fno=freefile() 'fno是文件号
do until eof(fno)
open "你的文本文档",for input as #fno
line input #fno,newline
loop
print newline 'newline是最后一行的行数.就等于一共有多少行了三.
close #fno
没有“放之四海而皆准”的代码。应该根据实际需求来确定方案。如果是小文件,可以:1 读入 TextBox 用 API 去行数。(加载 TextBox 不会很快)
2 逐行读入。(也不会很快)
3 读入字符串,用 Replace 将 VbLf 全部替换掉(""),比较替换前后的字符串大小。等等……
由于文件较小,对于人机界面,速度应该不是问题。如果是很大的文件,则可以用二进制方式打开,以每块 1024 - 4096 大小的缓存读入。然后查找缓存中 &HA 的个数。直到读完整个文件。
General\Open File Without Temp
选中该选项后还可以在下面填入
Threshold KB
表示大于指定大小的文件直接编辑,小于的还是可以用临时文件编辑的,很方便。