VB2008如何读取TXT文本的最后一行? 20
如下图所示,按下读取按钮后最后一行的cccccppppooooooo分别显示在3个TextBox里面...
如下图所示,按下读取按钮后 最后一行的ccccc pppp ooooooo 分别显示在3个TextBox里面
展开
2个回答
展开全部
VB2008读取TXT文本的最后一行:
Function GetFileLastLine(fName As String) As String
'fName 要处理的文件路径和名称
Dim hFile As Long
Dim recNumber1 As Long, recNumber2 As Long
Dim Char As String * 1
Dim strTmp As String
recNumber2 = FileLen(fName) '用于定位到最后一个字符
hFile = FreeFile
Open fName For Binary As hFile
'因为最后一行可能包含空格符回车符等
'所以要先找到最后一个有效字符
'有效字符条件要根据实际情况定,本示例定为字母和数字
Do
Get #hFile, recNumber2, Char
recNumber2 = recNumber2 - 1
Loop Until Char Like "[0-9a-zA-Z]"
'从最后一个有效字符起,再向前找最后一个换行符或回车符
recNumber1 = recNumber2
Do
Get #hFile, recNumber1, Char
recNumber1 = recNumber1 - 1
Loop Until Char = vbCr Or Char = vbLf
'读取最后一行
strTmp = Space(recNumber2 - recNumber1 + 1)
Get #hFile, recNumber1 + 1, strTmp
Close
GetFileLastLine = strTmp
End Function
'调用:
Private Sub Command1_Click()
Debug.Print GetFileLastLine("D:\db_backup\20100818p.txt") '883,999KB
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询