Reading block of text from file

P

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.
 
S

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top