Solved

Reading data from files and Zones

Posted on 2006-10-20
7
193 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

25 Experts available now in Live!

Get 1:1 Help Now