VB6.0读取TXT内容

TXT内容如下,把TXT每行的第二个和第三个逗号之间的数据存入数组SZtxt()中。
Circumference 1, 00, 06258, 1, 65535,
Pulses per Rev. 1, 01, 020000, 1, 999999,
Circumference 2, 02, 8278, 1, 65535,
Trim Time, 04, 002, 0, 999,
'------------------------------------------------------------------
SZtxt(1)=06258
SZtxt(2)= 020000
SZtxt(3)= 8278
SZtxt(4)=002

TXT指什么?是文本文件还是文本框?
文本文件:

Option Explicit
Private Sub Command1_Click()
Dim SZtxt() As String
Dim d() As String
Dim n As Integer
Dim LinStr As String
Dim i As Integer
n = 0
Open "c:\1122.txt" For Input As #1 '以读的方式打开文件
Do While Not EOF(1) ' 循环至文件尾
n = n + 1
ReDim Preserve SZtxt(1 To n) As String '数组增加1个元素
Line Input #1, LinStr '读入一行用户名
d = Split(LinStr, ",")
SZtxt(n) = d(2)
Loop
Close #1 ' 关闭文件。
End Sub

文本框:
Dim SZtxt() As String
Dim d() As String
Dim ls() As String
Dim LinStr As String
Dim i As Integer
LinStr = Trim(Text1.Text)
ls = Split(LinStr, vbCrLf)
ReDim SZtxt(UBound(ls)) As String
For i = 0 To UBound(ls) - 1
d = Split(ls(i), ",")
SZtxt(i + 1) = d(2)
Next i追问

TXT文本文件里面的数据

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-23
'假如文件放在D盘,名字叫ABC.txt
Private Sub Command1_Click()
Dim str(4) As String, SZtxt(4) As String
Dim n As Integer

Open "D:\ABC.txt" For Input As #1
While Not EOF(1)
n = n + 1
Line Input #1, str(n)'将文件中字符串整行的读入变量str(n)中
Wend
Close 1

Dim n1 As Integer, n2 As Integer, n3 As Integer
For i = 1 To 4
n1 = InStr(str(i), ",") '每行中第一个逗号的位置
n2 = n1 + 4'第二个逗号的位置
n3 = InStr((n2 + 1), str(i), ",")'第三个逗号的位置
SZtxt(i) = Mid(str(i), n2 + 1, n3 - n2 - 1)'在第二个逗号和第三个逗号之间取出目标字符串
Print SZtxt(i)
Next
End Sub
第2个回答  2013-04-23
Dim SZtxt(0 To 5) As String
Dim str As String
Dim hf As Integer: hf = FreeFile
Dim iFIle As Integer
Dim Lines

Open "c:\1.txt" For Input As #hf
Lines = Split(Input$(LOF(hf), #hf), vbNewLine)
Close #hf

For iFIle = 0 To UBound(Lines)
If Trim(LCase(Lines(iFIle))) <> "" Then
SZtxt(iFIle) = Split(Trim(LCase(Lines(iFIle))), ",")(2)
End If
Next iFIle追问

SZtxt(iFIle) = Split(Trim(LCase(Lines(iFIle))), ",")(2)

下标越界??

相似回答