怎么在TXT文件中读取数据,并存入VB数组中
答案:4 悬赏:0 手机版
解决时间 2021-03-28 12:34
- 提问者网友:低吟詩仙的傷
- 2021-03-27 13:31
怎么在TXT文件中读取数据,并存入VB数组中
最佳答案
- 五星知识达人网友:白昼之月
- 2021-03-27 14:23
如果TXT文件是单行的话
Open "c:\1.txt" For Binary As #1
Text1.Text = Input(lof(1), 1)
Close #1
Dim a
a = Split(Text1.Text)
如果是多行要加循环
Option Explicit
Dim a$, n%, k%, c$(), d%
Private Sub Form_Click()
n = 0
Open App.Path & "\temp.txt" For Input As #1
Do Until EOF(1)
n = n + 1
Line Input #1, a
ReDim c(n)
d = Len(a)
k = InStr(1, a, "=")
c(n) = Right(a, (d - k + 1))
Print "c(" & n & ")=" & c(n)
Loop
Close #1
End Sub
Open "c:\1.txt" For Binary As #1
Text1.Text = Input(lof(1), 1)
Close #1
Dim a
a = Split(Text1.Text)
如果是多行要加循环
Option Explicit
Dim a$, n%, k%, c$(), d%
Private Sub Form_Click()
n = 0
Open App.Path & "\temp.txt" For Input As #1
Do Until EOF(1)
n = n + 1
Line Input #1, a
ReDim c(n)
d = Len(a)
k = InStr(1, a, "=")
c(n) = Right(a, (d - k + 1))
Print "c(" & n & ")=" & c(n)
Loop
Close #1
End Sub
全部回答
- 1楼网友:独钓一江月
- 2021-03-27 16:09
应该是用INPUT这个函数吧,你在MSDN上找一个这个函数的用法,至于给数组赋值还得考虑数据类型的转换,因为从TXT读出来的是字符串。
- 2楼网友:思契十里
- 2021-03-27 15:49
dim SS$()
Private Sub Command1_Click()
dim A$,i%
list1.clear
Open "文件名" For Input As #1
While Not EOF(1)
line Input #1, A$'可以考虑行读入
List1.AddItem A$
Wend
Close
redim SS(List1.listcount)
for i=0 to List1.listcount -1
SS(i)=list1.list(i)
next
End Sub
Private Sub Command1_Click()
dim A$,i%
list1.clear
Open "文件名" For Input As #1
While Not EOF(1)
line Input #1, A$'可以考虑行读入
List1.AddItem A$
Wend
Close
redim SS(List1.listcount)
for i=0 to List1.listcount -1
SS(i)=list1.list(i)
next
End Sub
- 3楼网友:撞了怀
- 2021-03-27 15:39
Private Sub Command1_Click()
dim sj(1000)'数自己调吧
Open "文件名" For Input As #1
While Not EOF(1)
Input #1, sj(i)'可以考虑行读入
i = i + 1
Wend
Close
End Sub追问这样最后读完数据了怎么计数啊,需要知道最后数组里有多少项的追答那个i是计数器啊,从0开始计的,i+1 就是项数了。可以print i+1显示出来
dim sj(1000)'数自己调吧
Open "文件名" For Input As #1
While Not EOF(1)
Input #1, sj(i)'可以考虑行读入
i = i + 1
Wend
Close
End Sub追问这样最后读完数据了怎么计数啊,需要知道最后数组里有多少项的追答那个i是计数器啊,从0开始计的,i+1 就是项数了。可以print i+1显示出来
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯