Open "C:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, a
Loop
Close #1
a1 = Mid(a, 17, 6)
a1 = Mid(a1, 2, 1) & Mid(a1, 4, 1) & Mid(a1, 6, 1)
Label1.Caption = a1改代码可以读取最后一行的数据,但是如果有空行之类的就能读取了。文本文件的格式如下:AC7664340100000000020484
AC7664340100000000020584
AC7664350100000000020384AC7664350100000000030384*********************
当中可能有其他字符,需要分条件的读,请问该如何更改程序
目前思路是 instr(a,"AC766434") 然后 instr(a,"AC766435") 这2个字段分开来读取
Do While Not EOF(1)
Line Input #1, a
Loop
Close #1
a1 = Mid(a, 17, 6)
a1 = Mid(a1, 2, 1) & Mid(a1, 4, 1) & Mid(a1, 6, 1)
Label1.Caption = a1改代码可以读取最后一行的数据,但是如果有空行之类的就能读取了。文本文件的格式如下:AC7664340100000000020484
AC7664340100000000020584
AC7664350100000000020384AC7664350100000000030384*********************
当中可能有其他字符,需要分条件的读,请问该如何更改程序
目前思路是 instr(a,"AC766434") 然后 instr(a,"AC766435") 这2个字段分开来读取
Open "C:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, a
If InStr(a, "AC766434") > 0 Then
a1 = Mid(a, 17, 6)
a1 = Mid(a1, 2, 1) & Mid(a1, 4, 1) & Mid(a1, 6, 1)
Label1.Caption = a1
ElseIf InStr(a, "AC766435") > 0 Then
a1 = Mid(a, 17, 6)
a1 = Mid(a1, 2, 1) & Mid(a1, 4, 1) & Mid(a1, 6, 1)
Label2.Caption = a1
End If
Loop
Close #1
Dim Str$, Arr
Open "C:\1.txt" For Input As #1 Str = StrConv(InputB(LOF(1), 1), vbUnicode): Reset
Str = Trim(Replace(Str, vbCrLf, " "))
Arr = Split(Str, " ")
Str = Arr(UBound(Arr))
Label1.Caption = Mid(Str, 18, 1) & Mid(Str, 20, 1) & Mid(Str, 22, 1)
End Sub
Dim Str$, Arr
Open "C:\1.txt" For Input As #1
Str = StrConv(InputB(LOF(1), 1), vbUnicode): Reset
Arr = Split(Str, vbCrLf)
Str = Arr(UBound(Arr))
Label1.Caption = Mid(Str, 18, 1) & Mid(Str, 20, 1) & Mid(Str, 22, 1)
End Sub
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 - Len("AC7664350100000000030384")
'否则如果最后一行非定长字符串下处理,也是通用的方法
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 = strTmpEnd FunctionPrivate Sub Command1_Click()
Debug.Print GetFileLastLine("D:\db_baukup\20100716.txt") '883,999KB
End Sub
用于测试的文件883,999KB...只是个思路,想那儿写那儿了,代码要完善优化....
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)
Get #hFile, recNumber1 + 2, strTmp
Close
GetFileLastLine = strTmp
End Function
自己测试吧,思路如此了..
干活了....
看不懂你说什么。是取非空的最后一行吗? Open "C:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
If tmp > "" Then a = tmp
Loop
Close #1
a1 = Mid(a, 17, 6)
a1 = Mid(a1, 2, 1) & Mid(a1, 4, 1) & Mid(a1, 6, 1)
Label1.Caption = a1