file io问题--在读取行时没有写入文件

问题描述 投票:0回答:1

我有一个Things的文本文件,每个Things都有一些相关的参数,像这样。

1   Thing 1
2   Attribute: X
3   Other1: foo
4   Other2: bar
5   Thing 2
6   Attribute: Y
7   Other1: foo
8   Thing 3
9   Attribute: a
10  Thing 4
11  Attribute: f
12  Thing 5
13  Attribute: m
14  EndText

其中Things的编号表示属性在以下数组中的位置。

Current Attribute Array: [X, Y, a, f, m]
Future Attribute Array: [X - 1, Y - 1, a - 1, f - 1, m - 1] ' (representing some mathematical operation based on conditions)

(这些数组在代码中已经存在)

假设上面的文件叫做Things.txt。我希望能够在文件中逐行修改 "Attribute",将当前数组中的值改为未来数组中的操作属性。

请注意,"OtherN "参数是为了强调文件中每个 "Thing "的部分在行数上是不相等的。

我设置了一个for循环,从lbound(currentArray)到ubound(currentArray)

假设我们在循环迭代3中

我之前已经设置了

rowThing = 8 (the row that Thing 3's section begins)
rowNextThing = 10 (the first row that no longer applies to Thing 3)
searchText = "Attribute: a" (a concatenation of "Attribute: " and the 3rd entry in the current attribute array)
Set fso = CreateObject("Scripting.FileSystemObject")
Open filePath For Input As #1
Open tempFilePath For Append As #2

    Do While Not EOF(1)

        Line Input #1, textline

        rowCounter = rowCounter + 1

        ' check the current line to see if it contains the string we're looking 
        ' for (must be in between rowThing and rowNextThing and equal to the 
        ' search text. If so, change whats printed in the tempFile. If not, 
        ' just read from file and write to tempFile
        If (InStr(textline, searchText) > 0 And rowCounter > rowThing And rowCounter < rowNextThing) Then

            Print #2, futureAttribute ' ("Attribute: a - 1", in this case)

        Else

            Print #2, textline

        End If

    Loop

Close #1
Close #2

' delete file, rename tempFile to file (saving the newly written tempFile)
fso.DeleteFile filePath
fso.MoveFile tempFilePath, filePath

当我走过这段代码并到达 "Close #2 "时,我希望看到的是一个看起来和文件一模一样的tempFile。相反,我看到的是一个看起来很像文件开头的tempFile,但却很早就停止了(例如,只写到第10行)。

请注意,真正的文件是888行,有27个不同的Things。

有什么想法吗?

EDIT:有人要求我对我的代码提供一个更完整的交代,所以这里是

Set fso = CreateObject("Scripting.FileSystemObject")

rowThing = 0
rowNextThing = 0

' set arrays
currentValueAttributes = Array(9.9, 8.7, 4.5, 4.6, 10.1)
newValueAttributes = Array(9.7, 7.2, 5, 3.5, 7.8)
arrayThings = Array("Thing 1", "Thing 2", "Thing 3", "Thing 4", "Thing 5")

For i = LBound(arrayThings) To UBound(arrayThings) ' this should equate to for i = 0 to 4

    searchTextAttribute = "Attribute: " & currentValueAttributes(i)
    replaceTextAttribute = "Attribute: " & newValueAttributes(i)
    searchTextThing = arrayThings(i)

    If i < UBound(arrayThings) Then

        searchTextNextThing = arrayThings(i + 1)

    Else

        searchTextNextThing = "EndText"

    End If

    Open filePath For Input As #1

        Do While Not EOF(1)

            Line Input #1, textline

            rowCounter = rowCounter + 1

            ' check the current line to see if it contains arrayThings(i)
            ' if it does, set rowThing as the current line number (saves the rownum of the current Thing)
            If textline Like searchTextThing Then

                rowThing = rowCounter

            End If

        Loop

    Close #1

    rowCounter = 0
    textline = ""

        Open filePath For Input As #1

            Do While Not EOF(1)

            Line Input #1, textline

            rowCounter = rowCounter + 1

            ' check the current line to see if it contains arrayThings(i + 1) or EndText
            ' if it does, set rowNextThing as the current line number (saves the rownum of the current NextThing)
            If textline Like searchTextNextThing Then

                rowNextThing = rowCounter

            End If

            Loop

        Close #1

        rowCounter = 0
        textline = ""

    Open filePath For Input As #1
    Open tempFilePath For Append As #2

        Do While Not EOF(1)

            Line Input #1, textline

            rowCounter = rowCounter + 1

            ' check the current line to see if it contains the string we're looking 
            ' for (must be in between rowThing and rowNextThing and equal to the 
            ' search text. If so, change whats printed in the tempFile. If not, 
            ' just read from file and write to tempFile
            If (InStr(textline, searchTextAttribute) > 0 And rowCounter > rowThing And rowCounter < rowNextThing) Then

                Print #2, replaceTextAttribute

            Else

                Print #2, textline

            End If

        Loop

    Close #1
    Close #2

    ' delete file, rename tempFile to file (saving the newly written tempFile)
    fso.DeleteFile filePath
    fso.MoveFile tempFilePath, filePath

Next i
excel vba file-io
1个回答
0
投票

似乎你只需要读取一次文件,如果目的是根据你的数组值来替换内容的话。

我建议将配置(事物名称、当前值和新值)移动到工作表的范围中,然后用它来驱动你的代码。

例如:如果你的范围看起来像这样--

enter image description here

那么你的代码可以像这样(未经测试)。

Sub TESTER()

    Dim fso As Object
    Dim searchTextAttribute, replaceTextAttribute
    Dim ffI As Integer, ffO As Integer, inThing As Boolean
    Dim rngData As Range, m, filePath, tempFilePath, textline

    Set rngData = Sheet1.Range("A2:C12")

    filePath = "somePath"
    tempFilePath = "otherPath"

    ffI = FreeFile
    Open filePath For Input As #ffI
    ffO = FreeFile
    Open tempFilePath For Append As #ffO

    Do While Not EOF(1)
        Line Input #1, textline

        m = Application.Match(textline, rngData.Columns(1), 0) '<< is this a Thing?
        If Not IsError(m) Then
            'found a Thing, so read the current/new attributes
            searchTextAttribute = "Attribute: " & rngData.Cells(m, 2).Value
            replaceTextAttribute = "Attribute: " & rngData.Cells(m, 3).Value
            inThing = True
        Else
            If inThing And textline = searchTextAttribute Then
                textline = replaceTextAttribute
            End If
        End If
        Print #2, textline
    Loop

    Close #ffI
    Close #ffO

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.