VB读取DAT文件问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 15:15:52
我有一个DAT文件stulnfo.dat,数据前几条如下
999999999 里小东 False 21
111111111 莉莉 True 20
333333333 大卫 False 23
666666666 王维 False 22
然后代码如下
Open "d:\vb\stulnfo.dat" For Input As #1
ReDim myArray(LOF(1))
Do While Not EOF(1)
Input #1, myArray(i)
Loop
Close #1
For i = 1 To 10
Print myArray(i)
Next i
我执行后为什么是空的,什么也没显示出来??
哈哈我自己弄出来了!在不知道DAT文件有多少条数据的情况下
代码如下:
Dim myArray() As String
Private Sub Command1_Click()
Open "d:\vb\stulnfo.dat" For Input As #1
iline = 1
Do While Not EOF(1)
If EOF(1) = False Then
iline = iline
End If
For i = 1 To iline
ReDim myArray(iline)
Line Input #1, myArray(i)
Print myArray(i)
Next i

给两个通用函数你
自己看着用
你自己试试

msgbox openfile("读入文件的绝对路径")

Public Function openfile(ByVal filepath As String) As String
Dim s As String
Open filepath For Input As #1
While Not EOF(1)
Line Input #1, sline
s = s & sline & vbCrLf
Wend
Close #1
openfile = s
End Function

Public Function savefile(ByVal filepath As String, ByVal txt As String)
Open filepath For Output As #1
Print #1, txt
Close #1
End Function

Function disStr(ByVal vstr As String) As String '文件路径/\字符处理函数
If Right$(vstr, 1) <> "\" And Right$(vstr, 1) <> "/" And vstr <> "" Then
disStr = vstr & "\"
Else
disStr = vstr
End If
End Function

你循环错了
你用for好了
改成
Open "d:\vb\stulnfo.dat" For Input As #1
ReDim myArray(LOF(1))
For i = 1 To 4
L