How to read a text file to an array?

relentless32

New member
Joined
Feb 23, 2005
Messages
2
Programming Experience
Beginner
I have this text file below saved in my c: directory

FeedA:100:200:400:233:564:10:237200:400:233
FeedB:267:126:548:128:675:16:293:567:89
FeedC:162:654:876:918:727:81:891:75:554
FeedD:576:713:373:187:931:38:918:87:98
FeedE:102:650:896:908:729:813:791:55:87
FeedF:56:313:673:467:731:382:718:43:76
FeedG:12:54:889:978:726:834:892:32:432
FeedH:57:813:333:189:971:34:914:432:87


I am trying to read all of this data into an array, each colon represents a different element, but i am having problems.
I am doing this using the vb.net smart device application. I am prepared to store my text file differently if this helps, but i will need to perform calculations on elements within the array when it is all read in.
This is the code i have currently, it builds successfully and deploys successfully to the emulator, but then just as form1 should appear it displays an error.

My code:


Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Windows Form Designer generated code

Dim sr1 As StreamReader = File.OpenText("C:\feed.text")
Dim arrInfoTemp(9) As String
Dim arr1() As string 'i originally had this line as Dim arr1 as Array,
'but was told to change it to the above

Dim sStr1 As String
Dim iCount As Integer
Dim arrInfoCount As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
arrInfoCount = -10

Do Until sr1.Peek = -1
arrInfoCount += 10
sStr1 = sr1.ReadLine
arr1 = Split(sStr1, ":")
ReDim Preserve arrInfoTemp(arrInfoCount + 10)
For iCount = 0 To 9
arrInfoTemp(arrInfoCount + iCount) = arr1(iCount)
Next

ReDim Preserve arrInfoTemp(arrInfoCount + 9)
Loop
sr1.Close()

arrInfoCount = arrInfoTemp.GetUpperBound(0)
Dim arrInfo((arrInfoCount + 1) / 10 - 1, 10) As String
For iCount = 0 To (arrInfoCount + 1) / 10 - 1
For arrInfoCount = 0 To 9
arrInfo(iCount, arrInfoCount) = arrInfoTemp(iCount * 10 + arrInfoCount)
Next
Next
End Sub
End Class
 
Relentless,

This is a problem I face with a lot of my code. The best method I found was to use the Streamreader (see below) and Split the code using the colon as the delimeter. Here is an example from a piece of my code:

Try

NumLines = 0
sr =
New StreamReader(LogFile)
temp = sr.ReadLine()
While Not temp Is Nothing
If Not temp Is Nothing Then
ALine = Split(temp, ",") 'ALine is a string array
End If
End While
sr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

I have left out some of the processing work that I do, but this should show you the basics of what you need to read it in and then split each line up into an array. If you have any questions or want additional help leave me a message.

CuJo
 
Back
Top