vb 写txt文件

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:18:29
读取完后
Dim a As String '定义内容
Dim b As String '定义临时变量
Open "2.txt" For Input As #1 '打开文件2
Do While Not EOF(1)'在文件尾部之前循环
Line Input #1, b
a = a & b & Chr(13) & Chr(10)
Loop
Text2.Text = a

以下为写入:
Private Sub Command2_Click()
Open "2.txt" For Output As #1
Print #1, text2.text
Close #1
End Sub
为什么写入的时候错误,提示文件以打开

文件通用读、写、追加函数

Sub Appdoc(ByVal docpath As String, ByVal txt As String)'追加记录函数
Open docpath For Append As #1
Print #1, txt
Close #1
End Sub

Public Function openfile(ByVal filepath As String) As String'读入文件函数
Dim s As String
Open filepath For Input As #1
While Not EOF(1)
Line Input #1, sline
s = s & sline & vbCrLf
Wend
Close #1
openfile = s
End Function

Public Function savefile(ByVal filepath As String, ByVal txt As String)'保存文件函数
Open filepath For Output As #1
Print #1, txt
Close #1
End Function