vb每秒读取一行文件数据问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 00:48:30
我想每隔1s钟读取Txt文件中的一行数据,即:第1s读取第一行数据存放在变量中,第二秒读取第二行,第三秒读取第三行数据,怎么办呢,txt数据可能有上千行。使用timer事件处理。
数据如下

"2007-04-26 23:59:00",79635,82.53975,9.391983,1,4473,86.37,-
"2007-04-27 00:00:00",79636,79.64361,7.653315,1,4473,86.37,-
"2007-04-27 00:01:00",79637,25.09981,16.32887,1,4473,86.37,-
"2007-04-27 00:03:00",79639,75.29942,7.49645,1,4473,86.37,-
"2007-04-27 00:04:00",79640,51.11672,20.86974,1,4473,86.37,-
"2007-04-27 00:05:00",79641,1.399803,4.009223,1,4473,86.37,-

确实应该先全部读到内存的,用窗体级的全局变量来处理,然后通过时钟来读取,不然每隔一秒就操作一次文件,上千行就是上千秒哦,简单过程如下:

Dim n As Integer, s(2000) As Integer
Private Sub Command1_Click()
Dim i As Integer
i = 0
Open "d:\abc.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, s(i)
i = i + 1
Loop
Close #1
n = i
Timer1.Enabled = True '把文件全部读到内存后,时钟开始
End Sub

Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
Dim t
Static j As Integer
t = s(j) '每隔1秒读取文件中的一行
j = j + 1
If j = n Then Timer1.Enabled = False '如果把文件全部读完,就让时钟停止
End Sub

夸张,倒不如一次过读入后内存处理