Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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
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
ssonia27
Asked:
ssonia27
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
arif_eqbalCommented:
Read the full line and Concatenate values so as to get the Bracketed part together

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

SS
0
 
arif_eqbalCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.

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

End Sub

Bob
0
 
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.

Bob
0
 
planoczCommented:
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
 
planoczCommented:
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
 
Bob LearnedCommented:
Options, always give 'em options, and let 'em decide for themselves.  Maybe, in the process, they can learn something new :)

Bob
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now