Brent Hacker
Brent Hacker

Reputation: 394

Move to next line in StreamReader

Is it possible inside a loop to either move to next line or temporarily read next line? I have not had much luck finding any useful data on how one would do this, my guess is somehow find the line number (index) of what line you are currently on and then read +1 from where you are.

Using TestFile As New IO.StreamReader(My.Settings.cfgPath & "tempRPT.txt", System.Text.Encoding.Default, False, 4096)
        Do Until TestFile.EndOfStream
            ScriptLine = TestFile.ReadLine
            ScriptLine = LCase(ScriptLine)
            If InStr(ScriptLine, "update: [b") Then
                Dim m As Match = Regex.Match(ScriptLine, "\(([^)]*)\)")
                builder.AppendLine(m.Value)

                'This is where it would move to next line temporarily to read from it
                If InStr(ScriptLine, "hive: write:") > 0 Or InStr(ScriptLine, "update: [b") > 0 Then 'And InStr(ScriptLine, "setmarkerposlocal.sqf") < 1 Then
                   builder.AppendLine(ScriptLine)

                End If
            End If

        Loop
    End Using

Upvotes: 1

Views: 4396

Answers (2)

John Alexiou
John Alexiou

Reputation: 29244

Try this. Real all the lines and put them in a Queue(Of T) object.

    Dim path As String = My.Settings.cfgPath & "tempRPT.txt"

    Dim lines As String() = IO.File.ReadAllLines(path, System.Text.Encoding.Default)
    Dim que = New Queue(Of String)(lines)

    Do While que.Count > 0
        ScriptLine = que.Dequeue()
        ScriptLine = LCase(ScriptLine)
        If InStr(ScriptLine, "update: [b") Then
            Dim m As Match = Regex.Match(ScriptLine, "\(([^)]*)\)")
            builder.AppendLine(m.Value)

            Dim next_line As String = que.Peek      'Read next line temporarily                'This is where it would move to next line temporarily to read from it
            If InStr(next_line, "hive: write:") > 0 Or InStr(next_line, "update: [b") > 0 Then 'And InStr(next_line, "setmarkerposlocal.sqf") < 1 Then
                builder.AppendLine(next_line)
            End If
        End If
    Loop

Upvotes: 3

NoAlias
NoAlias

Reputation: 9193

Assuming you're not using an old version of .Net, you may find it easier to just use a list of strings.

Dim lstLinesInTextFile As List(of String) = IO.File.ReadAllLines(My.Settings.cfgPath & "tempRPT.txt").ToList()

        Dim intCursor As Integer = 0
        For Each strLine As String In lstLinesInTextFile

            'Perform Logic Here

            'To View Next Line:
            If lstLinesInTextFile.Count > intCursor + 1 Then

                Dim strNextLine As String = lstLinesInTextFile(intCursor + 1)

                'Perform Logic Here.

            End If

            intCursor += 1

        Next

Upvotes: 1

Related Questions