VB如何读取txt数据?

假如有一个名为 007.txt 的文本。内容是:
第一行是 111 空格 222 空格 333
第二行是 444 空格 555 空格 666
请问我该如何实现,a=111,b=222,c=333,d=444,e=555,f=666 ?
跪求指点,本人不胜感激

Private Sub Command1_Click() '基本读取方法---只能读取非中文
' Dim a As String, b As String, c As String, d As String, e As String, f As String'定义几个变量
Dim FileNo As Integer
FileNo = FreeFile() '获取一个未使用的文件号
Dim str As String '用来记录最终的值
Open "你要读取文件的完整路径" For Input As #FileNo
While Not EOF(FileNo)
Dim str_Read As String
Input #FileNo, str_Read '读取一个字符到变量str_Read---不包含换行回车符,也不包括逗号,分号及Tab,当读到分隔符号(前面列举的4种)时就赋值给后面的变量,如果有多个变量就读取相对应多的分隔数据
'如果为你的007.txt文件且为你列举的内容那么上面一句就改成下面的语句
' Input #FileNo, a, b, c, d, e, f
' str = str & str_Read
Wend
'Me.Text1.Text = str
Close #FileNo
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-02
Private Sub Command1_Click()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
Dim f As String
Dim temp

Open App.Path & "\007.txt" For Input As #1

Line Input #1, x
temp = Split(x, " ")
a = temp(0)
b = temp(1)
c = temp(2)

Line Input #1, x
temp = Split(x, " ")
d = temp(0)
e = temp(1)
f = temp(2)

Close #1
End Sub
第2个回答  2011-06-02
Private Sub Command1_Click()
Dim mArr(), m%, n%, k%, Tmp$, Tmp2
m = 1
Open "e:\007.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, Tmp
Tmp2 = Split(Tmp, " ")
For n = 0 To UBound(Tmp2)
ReDim Preserve mArr(1 To m)
mArr(m) = Tmp2(n)
m = m + 1
Next n
Loop
Close #1

For n = 1 To m - 1
Print mArr(n)
Next
End Sub
第3个回答  2011-06-02
Private Sub Form_Load()
Dim a as string
Dim b as string
Dim c as string
Dim d as string
Dim e as string
Dim f as string
On Error Resume Next
Open App.Path & "\007.txt" For Input As #1
Do Until EOF(1)
Line Input #1, x
For Each i In Split(x, vbCrLf) '按行读数据,去掉空格
if (i=0) then '这里I应该代表行,是从1还是0开始我既不大清了,如果有错误你就改改
a=Left(x,3)
b=Mid(x,4,3)
c=Right(x,3)
end if
if (i=1) then '这里I应该代表行,是从1还是0开始我既不大清了,如果有错误你就改改
d=Left(x,3)
e=Mid(x,4,3)
f=Right(x,3)
end if
Next
Loop
Close #1
End Sub

上面转换成了字符串,如果a,b,c,d,e,f你需要转换成整形,你自己改吧,这个应该没问题了吧
第4个回答  2011-06-02
Dim str1 As Variant
Dim str2 As String
Open "文件路径" For Input As #1 '
While Not EOF(1)
Line Input #1, str2
str1 = Split(str2, " ")
Print str1(0), str1(1), str1(2) '这里就是你说的a,b,c,有下一行的话,继续赋值给这3个变量
Wend
Close #1
第5个回答  2011-06-02
Private Sub Command1_Click()
Dim a As String
Dim b As String
Dim c As String
Dim d As String
Dim e As String
Dim f As String
Dim temp

Open App.Path & "\007.txt" For Input As #1

Line Input #1, x
temp = Split(x, " ")
a = temp(0)
b = temp(1)
c = temp(2)

Line Input #1, x
temp = Split(x, " ")
d = temp(0)
e = temp(1)
f = temp(2)

Close #1
End Sub
第6个回答  2011-06-02
Private Sub Command1_Click()
Dim mArr(), m%, n%, k%, Tmp$, Tmp2
m = 1
Open "e:\007.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, Tmp
Tmp2 = Split(Tmp, " ")
For n = 0 To UBound(Tmp2)
ReDim Preserve mArr(1 To m)
mArr(m) = Tmp2(n)
m = m + 1
Next n
Loop
Close #1

For n = 1 To m - 1
Print mArr(n)
Next
End Sub
第7个回答  2011-06-02
Private Sub Form_Load()
Dim a as string
Dim b as string
Dim c as string
Dim d as string
Dim e as string
Dim f as string
On Error Resume Next
Open App.Path & "\007.txt" For Input As #1
Do Until EOF(1)
Line Input #1, x
For Each i In Split(x, vbCrLf) '按行读数据,去掉空格
if (i=0) then '这里I应该代表行,是从1还是0开始我既不大清了,如果有错误你就改改
a=Left(x,3)
b=Mid(x,4,3)
c=Right(x,3)
end if
if (i=1) then '这里I应该代表行,是从1还是0开始我既不大清了,如果有错误你就改改
d=Left(x,3)
e=Mid(x,4,3)
f=Right(x,3)
end if
Next
Loop
Close #1
End Sub

上面转换成了字符串,如果a,b,c,d,e,f你需要转换成整形,你自己改吧,这个应该没问题了吧
第8个回答  2011-06-02
Dim str1 As Variant
Dim str2 As String
Open "文件路径" For Input As #1 '
While Not EOF(1)
Line Input #1, str2
str1 = Split(str2, " ")
Print str1(0), str1(1), str1(2) '这里就是你说的a,b,c,有下一行的话,继续赋值给这3个变量
Wend
Close #1
相似回答
大家正在搜