用VB如何把文本文件中的数字存入数值数组,请高手指点下。
如E盘的文本文件中有以下数字,每个数字两边都有两个逗号,用VB如何只读取数字,并把数字存入数值数组。,25,
,30,
,10,
,23,
,22,
,31,
,2,
,2,
,8,
,15,
,12,
,8,
,4,
,27,
,30,
,18,
--------------------编程问答-------------------- 用split 函数,试试吧 --------------------编程问答-------------------- Open "E:\数据库.txt" For Input As #1
St = ""
Z = ""
Do Until EOF(1)
Line Input #1, Z
St = St & Z & Chr(13) '第一步
Loop
Close #1
shuzhu = Split(St, ",", -1) '第二步
Print shuzhu(0)
Print shuzhu(1)
Print shuzhu(2)
Print shuzhu(3)
Print shuzhu(4)
Print shuzhu(5)
Print shuzhu(6)
Print shuzhu(7)
Print shuzhu(8)
Print shuzhu(9)
Print shuzhu(10)
Print shuzhu(11)
Print shuzhu(12)
Print shuzhu(13)
用split 函数分割出来的,有些数组是空的,并且要把数组命名为字符串数组。我想要数组数组但是不会啊!求教! --------------------编程问答-------------------- 把软件工程放在你这E盘相同的目录然后运行
--------------------编程问答-------------------- --------------------编程问答-------------------- 结合楼上的易做图可以了,问题是 shuzhu 为字符串数组,请教下怎么样把它转化为数值数组?
Private Sub Command1_Click()
Dim Sz '数组
Dim Str '文件内容
Str = OpenFile(App.Path & "\文件1.txt")
Str = Replace(Str, ",", "") '把没用的数字替换掉
Sz = Split(Str, vbCrLf) '字符转成数组
MsgBox "数组的第一第二个内容是:" & Sz(0) & "--" & Sz(1)
MsgBox "数组的所有内容是:" & vbCrLf & Join(Sz, vbCrLf)
End Sub
'┏〓〓〓〓〓〓〓〓〓 OpenFile,start 〓〓〓〓〓〓〓〓〓┓
'[简介]:
'打开文件
Function OpenFile(FileName, Optional ErrInfo As String) As String
'[mycode_id:19],edittime:2012/9/5 19:48:31
On Error GoTo Err1
Dim Fs, TextFile
Set Fs = CreateObject("Scripting.FileSystemObject")
Set TextFile = Fs.OpenTextFile(FileName)
OpenFile = TextFile.ReadAll ' 读取整个文件的信息
Exit Function
Err1:
ErrInfo = Err.Description
End Function
'┗〓〓〓〓〓〓〓〓〓 OpenFile,end 〓〓〓〓〓〓〓〓〓┛
Private Sub Form_Click()
Dim shuzhu() As String
St = ""
Z = ""
Open "E:\数据库\尾数\数据库01\002\文件1.txt" For Input As #1
Do Until EOF(1)
Line Input #1, Z
St = St & Z & Chr(13) '第一步
Loop
Close #1
St = Replace(St, ",", "") '把没用的数字替换掉
shuzhu = Split(St, Chr(13), -1)'''问题是 shuzhu 为字符串数组,请教下怎么样把它转化为数值数组?
--------------------编程问答--------------------
Private Sub Command1_Click()
Open "F:\data.txt" For Input As #1
Dim shuzhu() As Single
Dim Count As Single
Count = 0
Do Until EOF(1)
Line Input #1, z
ReDim Preserve shuzhu(Count) As Single
shuzhu(Count) = CSng(Mid(z, 2, Len(z) - 2)) '第一种方法
Count = Count + 1
st = st & z & Chr(13) '第一步
Loop
Close #1
'shuzhu = GetData(st) '第二种方法
Print shuzhu(0)
Print shuzhu(1)
Print shuzhu(2)
Print shuzhu(3)
Print shuzhu(4)
Print shuzhu(5)
Print shuzhu(6)
Print shuzhu(7)
Print shuzhu(8)
Print shuzhu(9)
Print shuzhu(10)
Print shuzhu(11)
Print shuzhu(12)
Print shuzhu(13)
End Sub
Private Function GetData(ByVal Data As String) As Single()
Dim BakArr() As String
Dim DataArr() As Single
Dim i As Single
Data = Replace(Data, ",", "")
BakArr = Split(Data, Chr(13))
ReDim DataArr(UBound(BakArr)) As Single
For i = 0 To UBound(BakArr) - 1
DataArr(i) = CSng(BakArr(i))
Next
GetData = DataArr
End Function
补充:VB , 基础类