Solved

Reading data from files and Zones

Posted on 2006-10-20
7
192 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert to Hour/minute textbox and compare 3 35
Events in static methods 3 25
Getfiles in vb.net 28 22
Close word object 13 18
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now