关于读取文本文件到数组?

hityu 2003-12-14 12:44:31
现欲读取内容如同以下的纯文本文件(txt):

1,5,7,9,10,38,38,39,29,37,17,11,3,5,7,9,
2,38,38,39,3,5,7,9,10,38,38,39,29,37,17,11,
3,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
4,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
5,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
6,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
7,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
8,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
9,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
10,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
11,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
12,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
13,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
14,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
15,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,
16,5,7,9,10,38,38,39,3,5,7,9,10,38,38,39,

其实就是逗号分隔的16*16矩阵。现在想实现:
1. 读取以上内容并将它存入二维数组中(integer型),具体如何实现?
2. 在控件上显示这些内容。要去掉逗号以空格代替,并且对齐(原数据长短不一,若依次连接显示,则行与行之间会对不齐)。用什么控件合适?

最好有较完整的source code,多谢!
问题解决后立即结贴 :-)

...全文
222 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodname008 2003-12-15
确实,简单的字符串分析。
回复
yoki 2003-12-15
Dim strTmp As String
Dim strArray() As String
Dim tmpArray() As String
Dim i As Integer, j As Integer
Dim strLine As String
Dim x As String

Open "e:\zz.txt" For Input As #1
strTmp = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
strArray = Split(strTmp, vbCrLf)
For j = 0 To UBound(strArray)
tmpArray = Split(strArray(j), ",")
For i = 0 To 15
strLine = strLine & tmpArray(i) & Space(8 - Len(tmpArray(i)))
Next i
x = x & Chr(10) + Chr(13) & strLine
strLine = ""
Erase tmpArray
Next j
Label1.Caption = x
回复
a11s 2003-12-15

Set lblNumber(i, j) = Controls.Add("VB.Label", "lblNumber" & i & j & i, Me)
这些好象提示出错
实时错误711
无效的类串
查找带ProgID:VB.Label的对象
回复
yoki 2003-12-15
用SPLIT分隔,然后另写入到文本即可
回复
jhzhou882 2003-12-15
确实简单
用SPLIT更简单
回复
TIANHEI 2003-12-14
用字符串函数一个一个找吧!
回复
zyl910 2003-12-14
简单的字符串分析而已
回复
rainstormmaster 2003-12-14
用多行文本框显示即可,当然要把字串格式化一下
回复
flc 2003-12-14
TO goodname008(卢培培,LPP Software) 好厉害!!!
想你学习!
关注!
回复
goodname008 2003-12-14
' 假设数据文件为c:\data.txt,用Label显示

Option Explicit

Private Sub cmdShow_Click()
Dim tmpData As String, strData As String, arrData(15, 15) As String, tmpArrData() As String
Dim i As Integer, j As Integer
Open "c:\data.txt" For Input As #1
Do Until EOF(1)
Line Input #1, tmpData
strData = strData & tmpData
Loop
Close #1

tmpArrData = Split(strData, ",")

For i = 0 To UBound(tmpArrData) - 1
arrData(Int(i / 16), Int(i Mod 16)) = Val(tmpArrData(i))
Next i

Dim lblNumber(15, 15) As Label
For i = 0 To 15
For j = 0 To 15
Set lblNumber(i, j) = Controls.Add("VB.Label", "lblNumber" & i & j & i, Me)
With lblNumber(i, j)
.AutoSize = True
.Left = j * 30 * 15 + 10 * 15
.Top = i * 30 * 15 + 10 * 15
.Caption = arrData(i, j)
.Visible = True
End With
Next j
Next i
End Sub

Private Sub Form_Load()
Me.Width = 600 * 15
Me.Height = 600 * 15
cmdShow.Left = (Me.Width - cmdShow.Width) / 2
cmdShow.Top = Me.Height - 100 * 15
cmdShow.Caption = "显示"
End Sub
回复
zxfvc 2003-12-14
带两颗星的说得很对,我以前就这么写的,满足功能。
回复
northwolves 2003-12-14
多行文本框显示:
Private Sub Command1_Click()
Text1.Text = ""
Text1.Width = 52 * Me.TextWidth(" ")
Dim txts As String, temp
Dim a(15, 15) As String
Open "d:\xxx.txt" For Binary As #1
txts = Space(FileLen("d:\xxx.txt"))
Get #1, , txts
Close #1
txts = Replace(txts, vbCrLf, "")
temp = Split(txts, ",")
For i = 0 To 15
For j = 0 To 15
a(i, j) = temp(i * 15 + j)
a(i, j) = Space(2 - Len(a(i, j))) & a(i, j)
Text1.Text = Text1.Text & a(i, j) & " "
Next
Text1.Text = Text1.Text & vbCrLf
Next
End Sub
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告