Improve company productivity with a Business Account.Sign Up

x
?
Solved

Read text file as a segment of text at a time

Posted on 2004-11-01
11
Medium Priority
?
158 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
Comment
Question by:ssonia27
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

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

0
 

Author Comment

by:ssonia27
ID: 12461754
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
ID: 12461843
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ssonia27
ID: 12461912
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:
Bob Learned earned 252 total points
ID: 12462921
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
ID: 12462937
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
planocz earned 248 total points
ID: 12463129
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
ID: 12463257
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
ID: 12466029
Options, always give 'em options, and let 'em decide for themselves.  Maybe, in the process, they can learn something new :)

Bob
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

584 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