假如有文本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.
别且最后一行是空行,没有文字!我的要求是追加到原文件的下一行作为最后一行!!谢谢~~~
 

解决方案 »

  1.   


    open "D:\test.txt" for append as input #1
        Print #1,vbcrlf & "second sentence in this text."
    close #1
      

  2.   

    open "D:\test.txt" for append as input #1
        Print #1 ""
        print #1 "second sentence in this text."
    close #1
      

  3.   

    首先,楼主手敲的第一句有错,as后怎么会有Input,应该是这个吧:
    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
      

  4.   

    无论是用Print还是Write语句,系统都会在末尾插入一空行,这是VB的特性,无法改变。
    所以必须另外想办法:
    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
      

  5.   

      Dim FSO As New FileSystemObject
      Dim TXT As TextStream
      
      Set FSO = CreateObject("Scripting.FileSystemObject")
      Set TXT = FSO.OpenTextFile("您的文本文件的完整路径", ForAppending)
          TXT.WriteLine "你需要追加的字符串"
          TXT.Close
      Set MCF = Nothing
      Set FSO = Nothing这样应该也可以的。
      

  6.   

    open "D:\test.txt" for append as #1
      Print #1 ""
      print #1 "second sentence in this text.";
    close #1
      

  7.   

    Private Sub Command1_Click()
        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