reading .txt file in VB.NET

bizjosh

Member
Joined
Mar 16, 2005
Messages
23
Programming Experience
Beginner
I have a text file that has some fields with delimiter of ",". how do i pass each value inside the text file to individual label boxes i have..

i'm not sure how to continue, this is what i have..

Private Sub Phone_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim psrdCurrent As System.IO.StreamReader = New System.IO.StreamReader("C:\phone.txt")
Dim pstrLine, pstrFile As String
pstrLine = psrdCurrent.ReadToEnd()
Do Until pstrLine = Nothing
pstrFile += pstrLine & CrLf
pstrLine = psrdCurrent.ReadLine()
Loop
psrdCurrent.Close()
End Sub
End Class

Text File has:
3.33,4.44,5.55,6.66
1.11,2.22,3.33,4.44
6.66,7.77,8.88,9.99
..
..

I wish to assign example 3.33 to variables and 4.44 to another variable, how do i do that?
 
Try the following:

VB.NET:
Private Sub Phone_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
  Dim psrdCurrent As System.IO.StreamReader = New System.IO.StreamReader("C:\phone.txt")
  Dim pstrLine, pstrFile As String
  pstrLine = psrdCurrent.ReadLine
  Dim strValues() as String
  While psrdCurrent.Peek() > -1
	strValues = Split(pstrLine, ",")
	txtBox1.Text = strValues(0)
	txtBox2.Text = strValues(1)
	txtBox3.Text = strValues(2)
	txtBox4.Text = strValues(3)
  End While

  psrdCurrent.Close()
End Sub
 
Yep, it works! ty

Is streamreader able to read from .xls (excel) files in
different cols?

My project requires me to read from a rates.txt that
has different mobile plans per charge in $x.xx i.e

(peak, offpeak, interstate calls, overseas call)
0.05,0.02,0.10,0.33
0.06,0.03,0.16,0.34
0.07,0.05,0.12,0.35

and another file usage.xls which has the usage in mins

(customer_id, peak usage, offpeak usage, interstate usage, int usage)
DBS258FF,55,75,34,4
GRE385SF,24,56,56,47
DER483FJ,24,66,46,64
...

The customer is has a strict first 3 letters uppercase, 4-5th letters are
numeric and 7-8th letters are uppercase, how do i do validation to
ensure the customer_id's are good?

Also in some cases, the usage will be left blank to show
that the customer does not subscribe to interstate calls
or international calls i.e

DBS258FF,55,75,,4
DBS258FF,55,75,34,

Therefore there will be a blank, how do i recognise that
or replace it with '0' value when passing it into a string as above

thanks a mill! :)
 
First I do not believe that the streamreader will be able to read an Excel File (unless it is a csv file). You will have to look at other options for getting to that data.

For customerID validation since you have some nice strong rules that would be a good candidate for a Regular Expression.

For the Blank fields I created an example (of course I did this basing it off of our earlier example and will only work if it is in a plain text format)

VB.NET:
Private Sub Phone_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
  Dim psrdCurrent As System.IO.StreamReader = New System.IO.StreamReader("C:\phone.txt")
  Dim pstrLine, pstrFile As String
  pstrLine = psrdCurrent.ReadLine
  Dim strValues() as String
  While psrdCurrent.Peek() > -1
	strValues = Split(pstrLine, ",")
        For x As Integer = 0 To UBound(strValues) - 1
            If strValues(x) = "" Then
                strValues(x) = 0
            End If
        Next
            
	txtBox1.Text = strValues(0)
	txtBox2.Text = strValues(1)
	txtBox3.Text = strValues(2)
	txtBox4.Text = strValues(3)
        txtBox5.Text = strValues(4)

        pstrLine = psrdCurrent.ReadLine
  End While

  psrdCurrent.Close()
End Sub
 
Back
Top