Reading block of text from file



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
strLine = sr.ReadLine()
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.

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
outLine = String.Empty
'No a break so
'display the line and append it
'to the collector variable
outLine &= strLine
End If

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

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