2012-07-31 4 views
1

루프 내에서 다음 줄로 이동하거나 다음 줄을 일시적으로 읽을 수 있습니까? 나는 행운을 빌어 요 어떻게 행했는지에 대한 유용한 데이터를 찾지 못했다. 내 추측은 현재 행의 행 번호 (색인)를 찾은 다음 당신이있는 곳에서 +1을 읽는다.StreamReader의 다음 줄로 이동

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 
+0

전체 파일을'System.IO.File.ReadAllLines()'로 메모리에서 읽을 수 있습니다. – ja72

답변

3

시도해보십시오. 모든 라인을 진짜로하고 Queue(Of T) 오브젝트에 넣으십시오.

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 
+0

빈 콜렉션에서 '엿보기'를하지 않았는지 확인하십시오. – ja72

+0

이 답변과 NoAlias의 답변은 모두 훌륭한 것입니다. 어떻게 작동하는지 먼저 살펴 보겠습니다. 나는이 것을 인정한다 : –

+0

결국 나는 너의 것을 사용하는 것을 끝내었다. 답변 감사합니다! –

1

이전 버전의 .Net을 사용하지 않는다고 가정하면 문자열 목록을 사용하는 것이 더 쉽습니다.

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 
관련 문제