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

来源:百度知道 编辑:UC知道 时间:2024/05/18 12:23:29
比如说有一个文件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)

按列同样是设一个临时数组 每行按空格或者分隔符拆分后 以对应数组索引位置 写入新数组

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