Solved

Reading data from files and Zones

Posted on 2006-10-20
7
194 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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