如何快速定位文本文件的行 用VB代码

来源:百度知道 编辑:UC知道 时间:2024/05/14 07:13:51
如何用VB代码 直接定位文本文件的行,
比如 我要在a.txt中 的第69行插入某些字符。

或者 读出a.txt中 第2行的整行数据
对于下面的两个回答,还不是很满意
因为我现在用的就是下面两位的方法,没有更直接的了吗?

open "C:\1.txt" for input as #1
do while not eof(1)
i=i+1
line input #1,R
if i=69 then print R'打印第69行的整行
loop

Function ReadTxtLine(fPath As String, nLine As Integer) As String
'该函数用于读取fPath文件的第nLine行
'返回值说明: 如果返回 文件未找到,返回vbNullString
' 没有找到这一行,返回 ""
' 找到,返回相应的这一行的内容
Dim N As Integer, S As String
If Len(Dir(fPath)) = 0 Then
ReadTxtLine = vbNullString
Else
ReadTxtLine = ""
Open fPath For Input As #1
N = 0
While Not EOF(1)
N = N + 1
Line Input #1, S
If N = nLine Then
ReadTxtLine = S
End If
Wend
Close #1
End If
End Function

Function InsertTxtLine(fPath As String, ByVal nLine As Integer, InsertString As String) As Boolean
'该函数用于在fPath文件的第nLine行,插入一行内容InsertString