大家好,今天我们共同来学习用VB编程来实现对记事本文件进行读写程序的实现。学习编程,不仅需要用心去学,实际编写、反复练习、认真思考也是至关重要的。
图:用VB来实现记事本文件的读写程序
程序用到的知识
VB文件的基础知识内容
文件是相关记录的集合,是将计算机处理的信息永久保存在磁介质上的基本方法。VB中的文件有顺序文件、随机文件、二进制文件三种。
VB进行文件的读语句、写语句、关闭语句
不同类型的文件,读写语句也不尽相同。
基本的语句结构是:Open “文件名” For 模式 As [#] 文件号
关闭文件语句:Close #文件号
有了上面的基础知识内容后,下面我们就正式开始实现今天的程序。
1、读取Read.txt内容
Private Sub Command1_Click() '读取read.txt内容
Dim strA As String, ch As String
Text1.ForeColor = vbBlue
Open App.Path & '\read.txt' For Input As #1
ch = ''
Do While Not EOF(1)
Line Input #1, strA
ch = ch & strA & vbCrLf
Text1.Text = ch
Loop
Close #1
End Sub
2、读取write.txt内容(写入前)
Private Sub Command2_Click() '读取write.txt内容(写入前)
Dim strA As String, ch As String
Open App.Path & '\write.txt' For Input As #1
ch = ''
Do While Not EOF(1)
Line Input #1, strA
ch = ch & strA & vbCrLf
Text1.Text = ch
Loop
Close #1
End Sub
3、将编写内容保存到write.txt
Private Sub Command3_Click() '将内容写入write.txt
Open App.Path & '\write.txt' For Output As #2
Print #2, Text2.Text
Close #2
End Sub
4、读取write.txt内容(写入后)
Private Sub Command4_Click() '读取write.txt内容(写入后)
Dim strA As String, ch As String
Open App.Path & '\write.txt' For Input As #1
ch = ''
Do While Not EOF(1)
Line Input #1, strA
ch = ch & strA & vbCrLf
Text1.Text = ch
Loop
Close #1
End Sub
***温馨提示:本文由跟我学VB原创,如需转载请注明来源跟我学VB。***
联系客服