How to use “append” command on specific position in a text file using Visual Basic?

£可爱£侵袭症+ 提交于 2019-12-04 05:24:55

问题


Text file:

test1

test2

test3

test4

I want to write after "test2" - something. I tried first time to open the file and I read the position where is test2. After that I opened the text file in Append format but I don't know how to write on specific position.

I must to use next commands or others in Visual Basic:

  • for reading from a text file: Open Test_Filename For Input As #

  • for appending in a text file: Open Test_Filename For Append As #3

Any helps will be great.


Edit:

LineNum = 1
LineRead = 0
Test_Filename ="path/test.txt"

If Len(Dir$(Test_Filename)) = 0 Then
    MsgBox ("Cannot find the file")
End If

Open Test_Filename For Input As #3
Do While Not EOF(3)

    LineNum = 1
    strData1 = ""
    LineNext = 0

    For LineNum = 1 To 2

        If EOF(3) Then
            Exit For
        End If

        Line Input #3, strLine
        On Error GoTo 0 'reset error handling
        LineRead = LineRead + 1

        If Left(strLine, 4) = "test1" Then
                strData1 = "test1"
                LineNum = 1
        End If

        If (strData1 = "test1") And (Left(strLine, 2) = "test2") And (LineNum = 2) Then
                strData2 = strData2 & strLine + vbCrLf
                strData1 = ""
            Else
                strData2 = ""
        End If

    Next LineNum

Loop


Close #3

Open Test_Filename For Append As #3
    If (InStr(strData2, "test2") <> 0) Then
        Print #3, "Something"
        Else
        Print #3, "Error"

    End If
Close #3

In this code I want to write after "test2" and other condition "Something". I must to open the file twice time because when I open with "For Input" I can't to write in the file.

But in this situation I wrote on the end of the file "Something" and I must to write to a specific position, after "test2".


回答1:


You could use:

Print #3, "This is my text to write to file."

Example here. If you need more information that this, please post the code you have so far.


Edit:

Try this:

Sub testWriteFile()

    Const Test_Filename = "c:\testfile.txt"
    Const findText = "test2"  ' what to find in file
    Const insertText = "!!!SOMETHING!!!"  ' what to put in line after [findText]

    Dim LinesRead As Integer, LinesInserted As Integer, outputText As String

    'If file doesn't exist then exit sub
    If Dir(Test_Filename) = "" Then
        MsgBox ("Cannot find the file")
        Exit Sub
    End If

    Open Test_Filename For Input As #3
    Do While Not EOF(3)

            'read line from input file
            Line Input #3, strLine
            LinesRead = LinesRead + 1

            'write line to output string
            outputText = outputText & strLine & vbCrLf

            'if line says
            If LCase(Left(strLine, Len(findText))) = LCase(findText) Then
                    outputText = outputText & insertText & vbCrLf  ' "insert" this line at the current position of output string
                    LinesInserted = LinesInserted + 1
            End If
    Loop

    'close output file
    Close #3


    'replace output file with output string
    Open Test_Filename For Output As #3
    Print #3, outputText
    Close #3

End Sub


来源:https://stackoverflow.com/questions/47028831/how-to-use-append-command-on-specific-position-in-a-text-file-using-visual-bas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!