Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading data from files and Zones

Posted on 2006-10-20
7
Medium Priority
?
198 Views
Last Modified: 2010-04-23
I have a text file called DATA.TXT with the following info:

----------------------------------------------
Does
this
work
----------------------------------------------

I need to read the file and display the first three letters of each string.  The output should be:

----------------------------------------------
Doe
thi
wor
----------------------------------------------

I am using the following code, but am only getting the first full line (not the three letters only).  What am I missing?

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")
        Dim fmtStr As String = "{0, 3}"
        Dim line As String
        line = sr.ReadLine
        sr.Close()
        lstOutput.Items.Add(String.Format(fmtStr, line))
    End Sub

0
Comment
Question by:jlavetan
[X]
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
7 Comments
 
LVL 12

Accepted Solution

by:
guidway earned 200 total points
ID: 17778951
It may be easier to use:

        Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")
        Dim fmtStr As String = "{0, 2}"
        Dim line As String
        Dim tmpline as String
        line = sr.ReadLine
        tmpline = line.Substring(0, 3)
        sr.Close()
        lstOutput.Items.Add(tmpline)
0
 
LVL 12

Expert Comment

by:guidway
ID: 17778964
btw, you could then get rid of the fmtStr variable (accidentally left it in)
0
 
LVL 1

Author Comment

by:jlavetan
ID: 17780047
The only output is the first string.  I can't get it read the next line in the DATA.TXT file.

Then only output I get is:

Doe
0
Industry Leaders: 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!

 
LVL 7

Assisted Solution

by:Mohamed Zedan
Mohamed Zedan earned 200 total points
ID: 17780238

just use this

        Dim sr As IO.StreamReader = IO.File.OpenText("DATA.TXT")
        Dim fmtStr As String = "{0, 2}"
        Dim line As String
        Dim tmpline As String

        While sr.Peek <> -1
            line = sr.ReadLine
            tmpline = line.Substring(0, 3)
            lstOutput.Items.Add(tmpline)
        End While

        sr.Close()
0
 
LVL 1

Author Comment

by:jlavetan
ID: 17780747
Thanks to both of you :-)
0
 
LVL 12

Expert Comment

by:guidway
ID: 17780999
glad we were of assistance to you.
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17781990
you're welcome .. glad to be of service :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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