Solved

Read text file as a segment of text at a time

Posted on 2004-11-01
154 Views
Last Modified: 2010-04-24
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
245:2:10:0:1:2:3:4:5:6:7:8:9]

[246:1:2:No:Yes
246:2:10:0:1:2:3:4:5:6:7:8:9
246:4:public:private:friend:global
246:2:polymorphism:enumeration]
:
:
:
:

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

SS
0
Question by:ssonia27
    9 Comments
     
    LVL 19

    Expert Comment

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

    0
     

    Author Comment

    by:ssonia27
    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.

    SS
    0
     
    LVL 19

    Expert Comment

    by:arif_eqbal
    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.

    0
     

    Author Comment

    by:ssonia27
    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.

    0
     
    LVL 96

    Accepted Solution

    by:
    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)
          Debug.WriteLine(m.ToString)
        Next m

    End Sub

    Bob
    0
     
    LVL 96

    Expert Comment

    by:Bob Learned
    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.

    Bob
    0
     
    LVL 27

    Assisted Solution

    by:planocz
    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

            Try
                ' 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
                        Do
                            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)
            Finally
                ' Close the object if it has been created.
                If Not myStreamReader Is Nothing Then
                    myStreamReader.Close()
                End If
                StreamReaderReadCharFile = sData
            End Try
        End Function
    0
     
    LVL 27

    Expert Comment

    by:planocz
    Hey TheLearnedOne,

    I used yours and my code and my test works great going either way.
    Thanks for the reminder of using Regular Expressions.
    0
     
    LVL 96

    Expert Comment

    by:Bob Learned
    Options, always give 'em options, and let 'em decide for themselves.  Maybe, in the process, they can learn something new :)

    Bob
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now