Home All Groups Group Topic Archive Search About

Reading block of text from file

Author
11 Apr 2006 1:55 PM
Przemek
Hi, I'm trying to parse some text file, which contain blocks of text.
First my code:

Public Class Parser
Public Sub New(ByVal fs As String)
        Dim sr As StreamReader
        sr = My.Computer.FileSystem.OpenTextFileReader(fs)
        Dim strLine As String
        Dim outLine As String
        Dim lineCount As Short

        outLine = ""
        strLine = sr.ReadLine()
        Do Until strLine = "-}"

            outLine = outLine + strLine
            MsgBox(strLine)
            strLine = sr.ReadLine()
        Loop
        MsgBox(outLine)
        sr.Close()
End Class

The problem is, that the length of the text block is unknown. I only
know for sure, that each block is separated from the next one by two
chars "-}" in line. My code read first block, I've got my outLine
variable (I will pass it to another object, parse and load into
database), but I don't how to continue parsing my file starting from
the line after "-}" line.

Author
11 Apr 2006 2:06 PM
Stephany Young
Change the loop and the way you detect the break:

  'Read the first line from the file
  strLine = sr.ReadLine()
  'Keep looping untlil the end of the file
  While strLine IsNot Nothing
    If strLine = "-}" Then
     'We hit the break so
     'display the block thus far and
     'clear the collector variable ready
     ' for the next block
      MsgBox(outLine)
      outLine = String.Empty
    Else
      'No a break so
      'display the line and append it
      'to the collector variable
      MsgBox(strLine)
      outLine &= strLine
    End If

    'Read the next line from the file
    strLine = sr.ReadLine()
  End While
  sr.Close()

Show quoteHide quote
"Przemek" <prz***@gmail.com> wrote in message
news:1144763716.592062.175190@i39g2000cwa.googlegroups.com...
> Hi, I'm trying to parse some text file, which contain blocks of text.
> First my code:
>
> Public Class Parser
> Public Sub New(ByVal fs As String)
>        Dim sr As StreamReader
>        sr = My.Computer.FileSystem.OpenTextFileReader(fs)
>        Dim strLine As String
>        Dim outLine As String
>        Dim lineCount As Short
>
>        outLine = ""
>        strLine = sr.ReadLine()
>        Do Until strLine = "-}"
>
>            outLine = outLine + strLine
>            MsgBox(strLine)
>            strLine = sr.ReadLine()
>        Loop
>        MsgBox(outLine)
>        sr.Close()
> End Class
>
> The problem is, that the length of the text block is unknown. I only
> know for sure, that each block is separated from the next one by two
> chars "-}" in line. My code read first block, I've got my outLine
> variable (I will pass it to another object, parse and load into
> database), but I don't how to continue parsing my file starting from
> the line after "-}" line.
>
Author
11 Apr 2006 2:45 PM
Przemek
Thanks for help, now it works!

Przemek