Read text file as a segment of text at a time

How do I read a text file as a segment or block of text at a time instead of reading line by line.

for example my text file is like

[245:1:2:No title added entry:title added entry


What I want is to read each Brackets text together
How do I do this

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Read the full line and Concatenate values so as to get the Bracketed part together

ssonia27Author Commented:
don't  want to read file line by line.
I want to read several lines together like a block within brackets.
Is there any way to recognize the beginning and end of the block so that block can be read.

Try reading Char by Char then
and keep checking for Opening and Closing Bracket

Another way is to read the whole file in one go and then use regular Expression to match Brackets. But this will consume lot of memory if the file is too big.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ssonia27Author Commented:
yes my file is too big that's why I want to read block of text.
because by reading file line by line or char by char will take too much time.

Bob LearnedCommented:
You can use regular expressions.  Read the entire file into at one time, then parse using the MultiLine options.

Regular Expression:  ([\[])([\w\d\s:])+([\]])

Imports System.Text.RegularExpressions   <-- This goes as the first line in the file.

Public Sub ParseBlocks(ByVal input As String)

    Dim patt As String = "([\[])([\w\d\s:])+([\]])"
    Dim rx As Regex = New Regex(patt, RegexOptions.Multiline)

    For Each m As Match In rx.Matches(Input)
    Next m

End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
BTW, how big are you talking about?  I would worry about using too much memory, if it works.  Just make sure that it gets released when you are done.

Here is some code that readers the file into a stream buffer,
you can use sOldData to read each char from [ to ]

   Public Function StreamReaderReadCharFile(ByVal sFileName As String)
        Dim myStreamReader As StreamReader
        Dim myNextInt As Integer
        Dim sData As String
        Dim sOldData As String

            ' Create a StreamReader using a Shared (static) File class.
            myStreamReader = File.OpenText(sFileName)

            ' The Read() method returns an integer.
            myNextInt = myStreamReader.Read()
            ' The Read() method returns '-1' when the end of the
            '   file has been reached
            While myNextInt <> -1
                ' Convert the integer to a unicode Char and add it to sData var.
                If ChrW(myNextInt) = "[" Then
                        myNextInt = myStreamReader.Read()
                        sData += ChrW(myNextInt)
                    Loop Until ChrW(myNextInt) = "]"
                    sData = sData.Substring(0, sData.Length - 1)
                    sOldData = sData
                    sData = ""
                End If
                myNextInt = myStreamReader.Read()
            End While
        Catch exc As Exception
            MsgBox("File could not be opened or read." + vbCrLf + _
                "Please verify that the filename is correct, " + _
                "and that you have read permissions for the desired " + _
                "directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
            ' Close the object if it has been created.
            If Not myStreamReader Is Nothing Then
            End If
            StreamReaderReadCharFile = sData
        End Try
    End Function
Hey TheLearnedOne,

I used yours and my code and my test works great going either way.
Thanks for the reminder of using Regular Expressions.
Bob LearnedCommented:
Options, always give 'em options, and let 'em decide for themselves.  Maybe, in the process, they can learn something new :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.