Solved

Reading data from files and Zones

Posted on 2006-10-20
7
195 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 50 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
Independent Software Vendors: 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 50 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

696 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