再问个文件操作的问题!

jswgn201 2008-03-10 06:47:21
请问,当我将text中内容输入到文本文件后,如data.txt,将程序关闭,下次再打开程序继续输入文本,输入的文本如何在data.txt文件中另起一行开始保存输入的文本,上次输入的内容则全部保留。
...全文
87 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
of123 2008-03-11
不是这样吧?

Private Sub cmdWriteText_Click() '写如文件按钮
Open App.Path & "\data.txt" For Appand As #1 '以添加方式打开
Print #1, Text1.Text
Close #1
End Sub


回复
jswgn201 2008-03-11
恩恩,我的意思就是of123的意思,问题已经解决了,谢谢大家,我从中也学到了不少东西!!
回复
cbm6666 2008-03-10
Dim aa$, bb$
Private Sub Form_Load()
If Dir("c:\data.txt") <> "" Then '应该要加个判断
Open "c:\data.txt" For Input As #1
While Not EOF(1)
Line Input #1, bb
aa = aa & bb & vbCrLf
Wend
Close #1
Text1.Text = aa
If InStr(Right(Text1.Text, 2), vbCrLf) > 0 Then Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
Open "c:\data.txt" For Output As #1
Print #1, Trim(Text1.Text)
Close #1
End Sub
回复
cbm6666 2008-03-10
Dim aa$, bb$
Private Sub Form_Load()
Open "c:\data.txt" For Input As #1
While Not EOF(1)
Line Input #1, bb
aa = aa & bb & vbCrLf
Wend
Close #1
Text1.Text = aa
If InStr(Right(Text1.Text, 2), vbCrLf) > 0 Then Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 2)
Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Open "c:\data.txt" For Output As #1
Print #1, Trim(Text1.Text)
Close #1
End Sub

回复
Option Explicit

Dim filename As String
Private Function exist() As Boolean
exist = (Dir(filename) <> "")
End Function

Private Sub Form_Load()
Dim s As String
On Error Resume Next
filename = App.Path & "\data.txt"

If Not exist Then
Open filename For Binary As #1
Close #1
End If

Open filename For Input As #1
Do
Line Input #1, s
Text1.Text = Text1.Text & s & vbCrLf
Loop While Not EOF(1)
Close #1

Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Kill filename
Open filename For Binary As #1
Put #1, , Text1.Text
Close #1

End Sub
回复
cbm6666 2008-03-10
呵呵 抱歉, 搞错你的意思了...
回复
zdingyun 2008-03-10

Open "c:\data.txt" For Append As #1
Print #1, Text1.Text
Close
回复
cbm6666 2008-03-10
免分送你一个,下载后将下面两部份修改一下,可以完全符合你的需求

下载地址:
http://download.csdn.net/source/374797


'**************Form_Load事件中的修改
If jj > 0 Then Text1.Text = Mid(aa, jj + 6)

改为

If jj > 0 Then
Text1.Text = Mid(aa, jj + 6) & vbCrLf
Text1.SelStart = Len(Text1.Text)
End If


'**************** 尾部追加文件信息下面的修改
Open TargetFile For Binary Access Write As #1
Put #1, FileLen(TargetFile) + 1, "CBM666" & Txtstr
Close #1

改为

Open TargetFile For Binary Access Write As #1
Txtstr = Trim(Txtstr)
If InStr(Right(Txtstr, 2), vbCrLf) > 0 Then Txtstr = Mid(Txtstr, 1, Len(Txtstr) - 2)
Put #1, FileLen(TargetFile) + 1, "CBM666" & Txtstr
Close #1






回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7491

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-10 06:47
社区公告
暂无公告