读书人

VB读取“.txt”数据文件解决办法

发布时间: 2012-12-15 15:16:03 作者: rapoo

VB读取“.txt”数据文件
请问各位大虾,能否帮个忙?我想实现用VB读取“.txt”数据文件,要求从文件的最后一行开始读取,把每行的数值存入变量中,一直读到第一行?大家帮忙想想办法!
例如:“data.txt”文件内容如下:
-19
8
10
60
83
-47
97
...
-63
最好能给出相应的代码!谢谢!
另外,因为我有一个思路,可以从文件的第一行开始读,依次读到最后一行,然后把数值存入数组中;接着在后续的调用过程中,从后往前调用。但是因为对数组不熟悉,编程功力没达到,肯定大虾们给予指点!能给出相应的代码最好,十分感谢!

[最优解释]
读出的数据为:
97
-47
83
60
10
8
-19

Private Sub Command1_Click()
Dim FileName As String
FileName = App.Path & "\文件1.txt"
Dim str As String

Dim hFile As Integer
hFile = FreeFile
Open FileName For Binary As #hFile
str = StrConv(InputB(LOF(hFile), 1), vbUnicode)
Close hFile

Dim Sz
Sz = Split(str, vbCrLf)
Dim I As Long
Dim Ub
Ub = UBound(Sz)
For I = Ub To 0 Step -1
Debug.Print Sz(I)
Next
End Sub

[其他解释]
Public Function MyTxtReader(ByVal StrPath As String) As String
MyTxtReader = ""
If IO.File.Exists(StrPath) = True Then
Dim TxtReader As IO.StreamReader = New IO.StreamReader(StrPath, System.Text.Encoding.Default)
MyTxtReader = TxtReader.ReadToEnd
TxtReader.Close()
End If
End Function
[其他解释]
vb提供的顺序文件读写,就是你说的那样了
也提供了随机文件读写,看样子不适合你的文件

你都有思路了,就按你的思路写吧.数组就是数组,用几次就熟悉了.vb的随机帮助说的太详细了

读书人网 >VB

热点推荐