VB编程如何将*.txt文件中的数据读入到数组?

比如说有一个文件1.txt,内容是:
23
78
890
333
等上千个数,如何读入到一个数组a()中。
多谢,急需要!!!
按列读取数据

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

如果是按一行一行读取进数组 你可以用

dim a
a = split(openfile(App.Path & "\1.txt"),vbcrlf)

按列同样是设一个临时数组 每行按空格或者分隔符拆分后 以对应数组索引位置 写入新数组
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-03
如果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
第2个回答  推荐于2017-09-20
dim strline as string
dim a()
dim i as integer
open app.path +"\1.txt for input as #1
do until eof(#1)
i=i+1
line input #1,strline
redim preserve a(i)
a(i-1)=strline
loop本回答被提问者采纳
相似回答