vb 输入超出文件尾

来源:百度知道 编辑:UC知道 时间:2024/06/10 10:34:04
Dim s() As String
Private Sub xiandao()
For i = 1 To 2
Load Text1(i)
Text1(i).Visible = True
Text1(i).Left = Text1(i - 1).Width + Text1(i - 1).Left - 25
Load Text2(i)
Text2(i).Visible = True
Text2(i).Left = Text2(i - 1).Width + Text2(i - 1).Left - 25
Load Text3(i)
Text3(i).Visible = True
Text3(i).Left = Text3(i - 1).Width + Text3(i - 1).Left - 25
Load Text4(i)
Text4(i).Visible = True
Text4(i).Left = Text4(i - 1).Width + Text4(i - 1).Left - 25
Next i
End Sub
Private Sub Command1_Click()
filenum = FreeFile()
Open "C:\Documents and Settings\Administrator\桌面\vb\PB\bb1.txt" For Input As filenum
callji = Input$(LOF(filenum), filenum)
Close filenum
s = Split(callji, ",")
For i1 = 0 To 2
Text1(i1) = s(i1)
Next i1
For i2 = 3 To 5
Text2(i2 - 3) = s(i2)
Next i2
For i3 = 6 To 8
Text3(i3 - 6) = s(i3)

分析了一下,问题出在LOF和input上面,lof函数对于中文一个汉字得到的长度是2字节,但input函数(length, string)里的length对于中文只算一个字节,所以读文件会出现超出文件结尾的错误,简单修改了程序,测试通过

Dim callji, Str As String
callji = ""
Dim L As Long
Open "C:\2.txt" For Input As #1

Do While Not EOF(1)
Input #1, Str
callji = callji & Str & ","
Loop
Close #1

用这种方法其实可以省去后面的split分割,因为连续读的str已经将“,”分割开,为了后面的程序不作改动,我用& ","加上了

是汉字时,修改这一句命令:
callji = Input$(LOF(filenum)/2, filenum)
数字母或数字时,不用改
当汉字和数字、字母混合时,我用别的方法解决,肯定能解决,需要的话再联系!!