VB编程如何将*.txt文件中的数据读入到数组?
比如说有一个文件1.txt,内容是:2378890333等上千个数,如何读入到一个数组a()中。多谢,急需要!!!按列读取数据...
比如说有一个文件1.txt,内容是:
23
78
890
333
等上千个数,如何读入到一个数组a()中。
多谢,急需要!!!
按列读取数据 展开
23
78
890
333
等上千个数,如何读入到一个数组a()中。
多谢,急需要!!!
按列读取数据 展开
3个回答
展开全部
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 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)
按列同样是设一个临时数组 每行按空格或者分隔符拆分后 以对应数组索引位置 写入新数组
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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询