[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Read text file as a segment of text at a time

Posted on 2004-11-01
Medium Priority
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


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

Question by:ssonia27
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 19

Expert Comment

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


Author Comment

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.

LVL 19

Expert Comment

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.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

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.

LVL 96

Accepted Solution

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)
    Next m

End Sub

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.

LVL 27

Assisted Solution

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

            ' 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
LVL 27

Expert Comment

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.
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 :)


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 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