在vb中,如何把文本文档中的数据读入到二维数组中,并且使二维数组的下标不越界

如果谁能给我做出这道题,我会永远感激他的!!!!!!!!!!

第1个回答  2011-06-21
Private Sub Form_Click()
Dim temp As String, str As String
Dim w1 As Integer, w2 As Integer '维数
Dim a() As Integer, b() As Integer
Dim i As Integer, j As Integer, k As Integer
w1 = 0
w2 = 0
Me.Text1.Text = "矩阵的初始状态为:"+ vbCrLf
Me.Text2.Text = "转置后的矩阵:"+ vbCrLf
Open "d:\in6.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, temp
w1 = w1 + 1
k = 0
For j = 1 To Len(temp)
If (Mid(temp, j, 1) = " ") And (Mid(temp, j + 1, 1) <> " ") Then
k = k + 1
End If
Next
If k > w2 Then
w2 = k
End If
Loop
Close #1

ReDim a(w1 - 1, w2) As Integer
ReDim b(w2, w1 - 1) As Integer
w1 = 0
Open "d:\in6.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, temp
k = 1
w2 = 0
For i = 1 To Len(temp)
If (Mid(temp, i, 1) <> " ") Then
For j = i To Len(temp)
If (Mid(temp, j, 1) = " ") Or (j = Len(temp)) Then
str = ""
str = Mid(temp, i, j - i + 1)
a(w1, w2) = Val(str)
i = j
w2 = w2 + 1
Exit For
End If
Next
End If
Next
w1 = w1 + 1
Loop
Close #1
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
b(j, i) = a(i, j)
str = CStr(a(i, j))
str = str + " "
str = Mid(str, 1, 6)
Me.Text1.Text = Me.Text1.Text + str
Next
Me.Text1.Text = Me.Text1.Text + vbCrLf
Next
For i = 0 To UBound(b, 1)
For j = 0 To UBound(b, 2)
str = CStr(b(i, j))
str = str + " "
str = Mid(str, 1, 6)
Me.Text2.Text = Me.Text2.Text + str
Next
Me.Text2.Text = Me.Text2.Text + vbCrLf
Next
End Sub
记得 textbox 的 MultiLine 设置成ture

在vb中,如何把文本文档中的数据读入到二维数组中,并且使二维数组的下...
Private Sub Form_Click()Dim temp As String, str As String Dim w1 As Integer, w2 As Integer '维数 Dim a() As Integer, b() As Integer Dim i As Integer, j As Integer, k As Integer w1 = 0 w2 = 0 Me.Text1.Text = "矩阵的初始状态为:"+ vbCrLf Me.Text2.Text = "...

...帮忙啊:vb 中如何从txt文件中将数据读取出来并赋值给二维数组?
假设点击命令按钮Command1就将e:\\aa.txt读入p(i,j)中,Command1的代码如下(你照复制就是):Private Sub Command1_Click()Dim StrLine As String, a() As String, p(11, 5) As String, i As Long, j As Long Open "e:\\aa.txt" For Input As #1 For j = 0 To 5 Do Until EOF...

请问大神如何用VB将文本文件的数据存入二维数组
用一个数组保存全部数据,其中第一个元素表示你所要求的数组的编号,你可以按需提取。数组数据结构为 (1,.1000,.01917)(1,.2000,.01994)...(1,.8000,.04849)(2,.1000,.01920)(2,.2000,.01998)...(2,.8000,.04861)代码如下:Dim Arr() As Double Dim num As Integer Pri...

vb把text中数据储存到二维数组
我是在vb里调试通过的,哎,麻烦死了 Private Sub Command1_Click()Dim Strtemp As String, A() As Double '读入数据到数字a中 Dim X As Variant, uu As Integer Strtemp = Text1 X = Split(Strtemp, ",") 'uu = UBound(X)ReDim A(uu - 1, 0 To 1) As Double Dim St As Intege...

vb读取txt数据到二维数组
我先告诉你思想吧:文件是不太规矩的,用分割不是很好;1.读取文件,把整个内容读到一个字符串变量里(里边只有空格没有回车);2.把规矩的字符串分割到一维数组;3.把一维数组变成二维数组。

求关于vb怎么把文本框里输入的矩阵读入一个二维数组中保存下来
楼主可参考如下代码:Dim i As Integer, j As Integer, x(4, 4) As Integer Dim s() As String Dim ss() As String s = Split(Text1.Text, Chr(13) & Chr(10))For i = 0 To 4 ss = Split(s(i))For j = 0 To 4 x(i, j) = Val(ss(j))Next Next ...

VB从文本文件读取直接赋值给二维数组?
'取每一行的数据,共五行,所以一维数组b有五个元素,最大下标为4 for i=0 to ubound(b)a=split(b(i)," ")'对于每一个b的元素,取b中数据到a数组中,25列,所以数组a最大下标为24。for j=0 to ubound(a)z(i,j)=a(j)next i next i 'z(i,j)就是所得的二维数组 ...

vb 中怎么将数据文件读入到数组中
如数据粘贴在c:\\1.txt中 dim ins$,i%,data_ins(1000) as single ,temp_data() as single,new_data(1000,1000)为了保证读取全,定义大数组。也可前面先读一次获取行列数,然后按照读取的行列数定义数组(这里略掉)open "c:\\1.txt" for input as #1 do until eof(1)line input #1,ins...

VB从文本文件读取数据并赋给赋值给变量及二维数组
以下代码假设你文本文件是“data.txt”、位置在D盘下,以下代码写在一个命令按钮command1里【你直接复制过去运行即可、当运行完毕下列模块即把所有数据装入那些未知数里了】:Private Sub Command1_Click()Dim Strling As String, StrData As String, a As Long, aa As Long, x() As Single Dim ...

VBA批量导入文本文件,如何转换二维数组?
str = StrConv(InputB(LOF(1), 1), vbUnicode)Close #1 arr = Split(str, Chr(10)) '只会生成一维数组,如何变成二维数组录入到单元格中 ReDim brr(UBound(arr))For i = 0 To UBound(arr)brr(i) = UBound(Split(arr(i), "|"))Next i n = Application.WorksheetFunction.Max(brr)Re...

相似回答
大家正在搜