VB怎么读取文本内容特定一行的特定内容

比如mytext.txt中有:
ab
b1234dfd1
然后要读取文本第二行中123这几个数字

Private Sub Command1_Click()
Dim s() As String
Open "mytext.txt" For Binary As #1
s = Split(Input(LOF(1), #1), vbCrLf)
Close #1
Print "第2行的第2个字符起的3个字符是:";
Print Mid(s(1), 2, 3)
End Sub


s(1)中的1表示第2行(从0起算的),后面的2表示从第2个字符起算,最后的3表示取3个字符。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-31
Private Sub Command1_Click()
Dim ProFile() As Byte, a As String
Dim i As Double
i = FreeFile
Open "D:\mytext.txt" For Binary As #i
ReDim ProFile(1 To LOF(i))
Get #i, , ProFile
Close #i
a = StrConv(ProFile(), vbUnicode)
Dim n As Integer, p As Integer, q As String
p = -1
For n = 2 To 2 '后一个2是你要的行数,如果是第一行,把for结构和里面的都去掉
    p = InStr(p + 2, a, vbCrLf)
Next n
q = Mid(a, p + 3, 3) 'p+2是那一行的第一个字符,第二个是p+3,以此类推
Print q
End Sub

第2个回答  2013-05-31
用循环和 line input 一行一行读取..想要哪一行就把哪行留下,然后跳出循环
相似回答
大家正在搜