Reading in data from a text file

swu

Active member
Joined
Mar 26, 2005
Messages
33
Programming Experience
1-3
Is there a way to control which line the sr.readline reads? I can't seem to control which line it starts with. If I place an sr.readline in different locations it always seems to read the first line.

my second question is if I have about 20 or 30 numbers to read in from a text file should I read them all in to variables such as V1, V2, V3, V4, etc or just try and use the sr.readline as mentioned above.

my last question is if I do try to read them into multiple variables V1, V2, V3, etc. How would I dim each one before it is created. I want it to work weather there are 3 variables or 3,000, like so.

i = 1
dim V(i) as double
V1=sr.readline
i=i+1

I can handle all the code/error checking except for dim the variable part.

Thanks in advance,

swu
 
You can try something like this:

VB.NET:
[COLOR=Blue]Dim [/COLOR]FileName [COLOR=Blue]As String[/COLOR]
[COLOR=Blue]Dim [/COLOR]sr [COLOR=Blue]As[/COLOR] IO.StreamReader
[COLOR=Blue]Dim [/COLOR]splitChar [COLOR=Blue]As String[/COLOR] = vbCrLf
[COLOR=Blue]Dim [/COLOR]FileContent [COLOR=Blue]As String[/COLOR]
FileName = "c:\test.txt"
sr = [COLOR=Blue]New[/COLOR] IO.StreamReader(FileName)
FileContent = sr.ReadToEnd
[COLOR=Blue]Dim [/COLOR]v() [COLOR=Blue]As String[/COLOR] = FileContent.Split(splitChar)
[COLOR=Blue]For [/COLOR]i [COLOR=Blue]As Intege[/COLOR]r = 0 [COLOR=Blue]To[/COLOR] UBound(v)
    MessageBox.Show(v(i))
[COLOR=Blue]Next[/COLOR]
I am not sure how to split your file actually. This way I guess your file arrangement is like this:
1.25
10
6.75

If you want to split them by a space bar rather than a carriage return change the splitChar to " " instead. Anyway, if you're familiar with ArrayList, it is always recommended to use it :)

Hope this helps...
 
Back
Top