首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么定位文本最后一行

2012-01-29 
如何定位文本最后一行VB6如何定位文本最后一行,或者最后一个字,不用AtEndOfStream,有没有直接访问或者定位

如何定位文本最后一行
VB6如何定位文本最后一行,或者最后一个字,不用AtEndOfStream,有没有直接访问或者定位的?

[解决办法]
Function LastLinePosition(ByVal PathName As String)
Dim hFile As Integer
Dim n As Byte
Dim i As Long

hFile = FreeFile()
Open PathName For Binary Access Read As #hFile
i = LOF(hFile)
Do While i > 0
Seek #hFile, i
Get #hFile, , n
If (n = vbKeyReturn) Or (n = 10) Then
i = i + 1
Exit Do
End If

i = i - 1
Loop
Close #hFile

LastLinePosition = i
End Function
[解决办法]
Function LastLinePosition(ByVal PathName As String) As String
Dim tmpByte() As Byte, tmpLong As Long, tmpLong1 As Long, tmpLong2 As Long
Dim ReadLen As Long, hFile As Long, hOffset As Long

ReadLen = 512 '一次读512字节
ReDim tmpByte(ReadLen)
hFile = FreeFile()

Open PathName For Binary As #hFile
tmpLong = LOF(hFile) '得到长度
If tmpLong > ReadLen Then
Do
tmpLong2 = tmpLong2 + 1 '计算偏移量
hOffset = tmpLong - ReadLen * tmpLong2

'这里应该要加上对hOffset的判断,免得遇到只有一行的文件....
'自己写吧,不想写了,脑壳晕了.....@_@

Seek #hFile, hOffset
Get #hFile, , tmpByte '读入一定字节

For tmpLong1 = ReadLen To 0 Step -1 '找到回车或换行符
If tmpByte(tmpLong1) = 13 Or tmpByte(tmpLong1) = 10 Then Exit For
Next tmpLong1

If tmpLong1 > 0 Then '如果找到了就输出,没找到就继续....
LastLinePosition = tmpLong - ((ReadLen * (tmpLong2 - 1)) + ReadLen - tmpLong1)

Seek #hFile, LastLinePosition '测试是否正确找到
ReDim tmpByte(tmpLong - LastLinePosition)
Get #hFile, , tmpByte
Debug.Print StrConv(tmpByte, vbUnicode)

Exit Do
End If
Loop
Else
'文件长度小于ReadLen的情况...自己写吧....
End If
Close #hFile
End Function

热点排行