如何使用“附加"功能?使用Visual Basic在文本文件中的特定位置上执行命令? [英] How to use "append" command on specific position in a text file using Visual Basic?

查看:110
本文介绍了如何使用“附加"功能?使用Visual Basic在文本文件中的特定位置上执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本文件:

test1

test2

test3

test4

我想在"test2"之后写东西. 我第一次尝试打开文件,然后读取了test2的位置.之后,我打开了追加"格式的文本文件,但不知道如何在特定位置书写.

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.

我必须在Visual Basic中使用下一个命令或其他命令:

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

  • 用于读取文本文件:打开Test_Filename以输入为#

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

用于附加到文本文件中:打开Test_Filename附加为#3

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

任何帮助都会很棒.

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

在这段代码中,我想在"test2"和其他条件"Something"之后编写.我必须两次打开该文件,因为当我使用用于输入"打开时,我无法写入该文件.

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.

但是在这种情况下,我在文件"Something"的末尾写了字,必须在"test2"之后写到特定位置.

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

推荐答案

您可以使用:

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.

尝试一下:

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

这篇关于如何使用“附加"功能?使用Visual Basic在文本文件中的特定位置上执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆