|
-
Apr 14th, 2012, 06:13 PM
#1
Thread Starter
Junior Member
How to use a StreamReader to read....
How do I use a StreamReader to read the data (from a text file), parse it, and place it in a two dimensional array??
The data:
0,710,887,1130,525
710,0,1510,1610,515
887,1510,0,454,1070
1130,1610,454,0,1110
525,515,1070,1110,0
Note: the first row of data contains the distances from location1 to all five locations, the second row of data contains the distances from location2 to all five locations, and so on.
-
Apr 14th, 2012, 07:01 PM
#2
Re: How to use a StreamReader to read....
using a streamreader:
vb Code:
Dim fileName As String = "filename.txt"
Dim sr As New IO.StreamReader(fileName)
Dim linesCount As Integer = sr.ReadToEnd.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Count
sr = New IO.StreamReader(fileName)
Dim array2D(linesCount - 1, 4) As Integer
For l As Integer = 0 To linesCount - 1
Dim fields() As String = sr.ReadLine.Split(","c)
For x As Integer = 0 To fields.GetUpperBound(0)
array2D(l, x) = CInt(fields(x))
Next
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 14th, 2012, 08:25 PM
#3
Re: How to use a StreamReader to read....
 Originally Posted by .paul.
using a streamreader:
vb Code:
Dim fileName As String = "filename.txt"
Dim sr As New IO.StreamReader(fileName)
Dim linesCount As Integer = sr.ReadToEnd.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).Count
sr = New IO.StreamReader(fileName)
Dim array2D(linesCount - 1, 4) As Integer
For l As Integer = 0 To linesCount - 1
Dim fields() As String = sr.ReadLine.Split(","c)
For x As Integer = 0 To fields.GetUpperBound(0)
array2D(l, x) = CInt(fields(x))
Next
Next
Remember to close/dispose of your StreamReader as well 
I would have just made use of the Using keyword for that.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|