vb6 读取*.csv文件的最后一行数据问题

jacksir 2016-04-01 04:50:54
我需要取*.csv文件的最后一行数据,代码中如果csv文件是多行就可以读,但是如果csv文件只有一行就无法读取了,请各位帮忙看看:
Private Sub Command1_Click()
MsgBox Replace(getchar(GetFileLastLine(App.Path & "\9056_020.csv"), ",", 3, 4), "Operation=", "")
End Sub

Function GetFileLastLine(fName As String) As String
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 Function getchar(mstr As String, charx As String, bofnum As Integer, eofnum As Integer) As String
Dim x As Integer, strartnum As Integer, endnum As Integer
For i = 1 To Len(mstr)
If x = eofnum Then Exit For
If Mid(mstr, i, 1) = charx Then
x = x + 1
If x = bofnum Then
strartnum = i
ElseIf x = eofnum Then
endnum = i
End If
End If
Next i
getchar = Mid(mstr, strartnum + 1, endnum - strartnum - 1)
End Function


单行的csv文件会在Get #hFile, recNumber1, Char里报:实时错误‘63’ 记录号错误
请高手们帮忙看看如何修改,先在此谢谢合位!
...全文
314 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jacksir 2016-04-06
  • 打赏
  • 举报
回复
引用 3 楼 bakw 的回复:
[quote=引用 2 楼 snrtjat 的回复:] [quote=引用 1 楼 bakw 的回复:]

Function GetFileLastLine(fName As String) As String
    Dim hFile As Long,strTmp As String,Arr() As string,I As Long
    hFile = FreeFile
    Open fName For Binary As hFile
    strTmp=Space(LOF(hFile))
    Get #hFile,,strTmp
    Close #hFile
    Arr=Split(strTmp,vbCrLf)
    For I=Ubound(Arr) To 1 Step -1
      if Len(Arr(i))>0 Then Exit For
    Next
    GetFileLastLine = Arr(I)
End Function
首先谢谢你的回答,您只解决了只有一行的CSV文件问题,但是如果CSV文件的行数大于一行时,你就全部读出来了。 我将CSV文件的样式列出如下,请帮忙再看看,谢谢! Tester=T101, Cell=14,Date=20160226042748, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160226115857, Operation=Online, MaintenanceAction=SWRebootCell, EC=9960, By=6001566, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160227053720, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160301034451, Operation=Online, MaintenanceAction=SWRebootCell, EC=9921, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160314205317, Operation=Offline, EC=CRMP, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160315033657, Operation=Online, MaintenanceAction=SWRebootCell, EC=9951, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160401072007, Operation=Offline, EC=CRFI, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= 我要取出最后一行的内容: Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber=[/quote] 如果是全被读出来,说明你的csv文件分行符是chr(10)把Arr=Split(strTmp,vbCrLf)改成Arr=Split(strTmp,chr(10))就行了[/quote] 是的,谢谢您 !
笨狗先飞 2016-04-05
  • 打赏
  • 举报
回复
引用 2 楼 snrtjat 的回复:
[quote=引用 1 楼 bakw 的回复:]

Function GetFileLastLine(fName As String) As String
    Dim hFile As Long,strTmp As String,Arr() As string,I As Long
    hFile = FreeFile
    Open fName For Binary As hFile
    strTmp=Space(LOF(hFile))
    Get #hFile,,strTmp
    Close #hFile
    Arr=Split(strTmp,vbCrLf)
    For I=Ubound(Arr) To 1 Step -1
      if Len(Arr(i))>0 Then Exit For
    Next
    GetFileLastLine = Arr(I)
End Function
首先谢谢你的回答,您只解决了只有一行的CSV文件问题,但是如果CSV文件的行数大于一行时,你就全部读出来了。 我将CSV文件的样式列出如下,请帮忙再看看,谢谢! Tester=T101, Cell=14,Date=20160226042748, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160226115857, Operation=Online, MaintenanceAction=SWRebootCell, EC=9960, By=6001566, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160227053720, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160301034451, Operation=Online, MaintenanceAction=SWRebootCell, EC=9921, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160314205317, Operation=Offline, EC=CRMP, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160315033657, Operation=Online, MaintenanceAction=SWRebootCell, EC=9951, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160401072007, Operation=Offline, EC=CRFI, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= 我要取出最后一行的内容: Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber=[/quote] 如果是全被读出来,说明你的csv文件分行符是chr(10)把Arr=Split(strTmp,vbCrLf)改成Arr=Split(strTmp,chr(10))就行了
jacksir 2016-04-05
  • 打赏
  • 举报
回复
引用 1 楼 bakw 的回复:

Function GetFileLastLine(fName As String) As String
    Dim hFile As Long,strTmp As String,Arr() As string,I As Long
    hFile = FreeFile
    Open fName For Binary As hFile
    strTmp=Space(LOF(hFile))
    Get #hFile,,strTmp
    Close #hFile
    Arr=Split(strTmp,vbCrLf)
    For I=Ubound(Arr) To 1 Step -1
      if Len(Arr(i))>0 Then Exit For
    Next
    GetFileLastLine = Arr(I)
End Function
首先谢谢你的回答,您只解决了只有一行的CSV文件问题,但是如果CSV文件的行数大于一行时,你就全部读出来了。 我将CSV文件的样式列出如下,请帮忙再看看,谢谢! Tester=T101, Cell=14,Date=20160226042748, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160226115857, Operation=Online, MaintenanceAction=SWRebootCell, EC=9960, By=6001566, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160227053720, Operation=Offline, EC=CVCF, By=Same Error, Notes= Tester=T101, Cell=14,Date=20160301034451, Operation=Online, MaintenanceAction=SWRebootCell, EC=9921, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160314205317, Operation=Offline, EC=CRMP, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160315033657, Operation=Online, MaintenanceAction=SWRebootCell, EC=9951, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= Tester=T101, Cell=14,Date=20160401072007, Operation=Offline, EC=CRFI, By=Many Error, Notes= Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber= 我要取出最后一行的内容: Tester=T101, Cell=14,Date=20160401111227, Operation=Online, MaintenanceAction=SWRebootCell, EC=9954, By=0, Notes=, SwapFromMachine=, SwapFromCell=, CardSerialNumber=
笨狗先飞 2016-04-01
  • 打赏
  • 举报
回复

Function GetFileLastLine(fName As String) As String
    Dim hFile As Long,strTmp As String,Arr() As string,I As Long
    hFile = FreeFile
    Open fName For Binary As hFile
    strTmp=Space(LOF(hFile))
    Get #hFile,,strTmp
    Close #hFile
    Arr=Split(strTmp,vbCrLf)
    For I=Ubound(Arr) To 1 Step -1
      if Len(Arr(i))>0 Then Exit For
    Next
    GetFileLastLine = Arr(I)
End Function

7,763

社区成员

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

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