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

jacksir jacksir 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我需要取*.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’ 记录号错误
请高手们帮忙看看如何修改,先在此谢谢合位!
...全文
评论 4
收藏 已收藏
分享
返回顶部
jacksir
VB基础类 7,788 成员 197,583 内容