假如有文本test.txt中仅有一行数据如下:
one sentence in this text.要求用VB6将"second sentence in this text."追加到这个文本的第二行,追加后的test.txt共有两行:
one sentence in this text.
second sentence in this text.代码如下:open "D:\test.txt" for append as input #1
Print #1,"second sentence in this text."
close #1运行后的结果如下:
one sentence in this text.second sentence in this text.
别且最后一行是空行,没有文字!我的要求是追加到原文件的下一行作为最后一行!!谢谢~~~
one sentence in this text.要求用VB6将"second sentence in this text."追加到这个文本的第二行,追加后的test.txt共有两行:
one sentence in this text.
second sentence in this text.代码如下:open "D:\test.txt" for append as input #1
Print #1,"second sentence in this text."
close #1运行后的结果如下:
one sentence in this text.second sentence in this text.
别且最后一行是空行,没有文字!我的要求是追加到原文件的下一行作为最后一行!!谢谢~~~
open "D:\test.txt" for append as input #1
Print #1,vbcrlf & "second sentence in this text."
close #1
Print #1 ""
print #1 "second sentence in this text."
close #1
Open "D:\test.txt" For Append As #1至于楼主说的多出的空行,那是楼主对VB中print的语法了解不够。简单的说,你可在参数中用分号;控制不要换行,用逗号,控件输出制表符,所以你的代码可以这样改
Private Sub TestAppend()
Open "D:\test.txt" For Append As #1
Print #1, "" '换行
Print #1, "second sentence in this text.";
Close #1
End Sub
所以必须另外想办法:
Option ExplicitPrivate Sub Command1_Click()
Dim FileNumber As Long
FileNumber = FreeFile
Open "D:\test.txt" For Binary As #FileNumber
Put #FileNumber, Len("one sentence in this text.") + 1, vbCrLf
Put #FileNumber, , "second sentence in this text."
Close #FileNumber
End Sub
Dim TXT As TextStream
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TXT = FSO.OpenTextFile("您的文本文件的完整路径", ForAppending)
TXT.WriteLine "你需要追加的字符串"
TXT.Close
Set MCF = Nothing
Set FSO = Nothing这样应该也可以的。
Print #1 ""
print #1 "second sentence in this text.";
close #1
Dim temp As String
Open "c:\1212.txt" For Input As #1
temp = Input(LOF(1), 1): Close #1
If Right(temp, 2) <> vbCrLf Then
Open "c:\1212.txt" For Append As #1
Print #1, vbCrLf & "second sentence in this text.";
Else
Open "c:\1212.txt" For Output As #1
While Right(temp, 2) = vbCrLf
temp = Left(temp, Len(temp) - 2)
Wend
temp = temp & vbCrLf & "second sentence in this text."
Print #1, temp;
End If
Close #1
End Sub