再问个文件操作的问题!

jswgn201 2008-03-10 06:47:21
请问,当我将text中内容输入到文本文件后,如data.txt,将程序关闭,下次再打开程序继续输入文本,输入的文本如何在data.txt文件中另起一行开始保存输入的文本,上次输入的内容则全部保留。
...全文
113 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
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

用户 昵称 2008-03-10
  • 打赏
  • 举报
回复
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






7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧