VB中如何读取txt文件的数据
Dim A, S As String
Dim FreeNum As Integer
FreeNum = FreeFile
"Freenum表示一个空闲的文件号
open "D:/date.txt" for input as #FreeNum
"这步是打开“date.txt”,for input表示以输入方式(即读取文件)打开。如果要写入文件则应该用output或append。
Do Until eof(FreeNum) "循环,直到文件结尾。Eof函数用来判断文件是否读完
Line Input #FreeNum, A
S = S + vbNewLine + A "S用来保存整个文件
If A满足某个条件 And Not Eof(FreeNum) Then
Line Input #FreeNum, A "读取下一行的内容
Exit do "退出循环
End if
Loop
Close FreeNum
至于将一个新值写进第N行,则可以
Dim A, S, S1 As String
Dim FreeNum As Integer
FreeNum = FreeFile
"Freenum表示一个空闲的文件号
open "D:/date.txt" for input as #FreeNum
"这步是打开“date.txt”,for input表示以输入方式(即读取文件)打开。如果要写入文件则应该用output或append。
Do Until eof(FreeNum) "循环,直到文件结尾。Eof函数用来判断文件是否读完
Line Input #FreeNum, A
S1 = S1 + vbNewLine + A "S用来保存1到n-1行的内容,S1用来表示n行以后的内容
If A满足新写一行的条件 Then
S = S1
S1 = ""
End if
Loop
Close FreeNum
Open "D:/date.txt" For Output As FreeNum
"关闭文件之后重新以Output的模式打开。
Print #FreeNum, S
Print #FreeNum, 新插入一行的内容
Print #FreeNum, S1
Close FreeNum
即可
Dim FreeNum As Integer
FreeNum = FreeFile
"Freenum表示一个空闲的文件号
open "D:/date.txt" for input as #FreeNum
"这步是打开“date.txt”,for input表示以输入方式(即读取文件)打开。如果要写入文件则应该用output或append。
Do Until eof(FreeNum) "循环,直到文件结尾。Eof函数用来判断文件是否读完
Line Input #FreeNum, A
S = S + vbNewLine + A "S用来保存整个文件
If A满足某个条件 And Not Eof(FreeNum) Then
Line Input #FreeNum, A "读取下一行的内容
Exit do "退出循环
End if
Loop
Close FreeNum
至于将一个新值写进第N行,则可以
Dim A, S, S1 As String
Dim FreeNum As Integer
FreeNum = FreeFile
"Freenum表示一个空闲的文件号
open "D:/date.txt" for input as #FreeNum
"这步是打开“date.txt”,for input表示以输入方式(即读取文件)打开。如果要写入文件则应该用output或append。
Do Until eof(FreeNum) "循环,直到文件结尾。Eof函数用来判断文件是否读完
Line Input #FreeNum, A
S1 = S1 + vbNewLine + A "S用来保存1到n-1行的内容,S1用来表示n行以后的内容
If A满足新写一行的条件 Then
S = S1
S1 = ""
End if
Loop
Close FreeNum
Open "D:/date.txt" For Output As FreeNum
"关闭文件之后重新以Output的模式打开。
Print #FreeNum, S
Print #FreeNum, 新插入一行的内容
Print #FreeNum, S1
Close FreeNum
即可
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
copyright © 2008-2019 入门客AI创业平台 版权所有 备案号:湘ICP备2023012770号