d-deamon
10-18-2003, 07:51 AM
I have a code to load a textfile into an array but i dont know what im doing wrong. studied some examples on the board, but because of the fact that im using vb just for me second day i dont know what they mean =]
here is my code:
---------------------------------------------------------------------
Dim a As String
Dim s() As String
Dim i As Integer
i = 1
Open "e:\test.txt" For Input As #1
Do Until EOF(1)
Input #1, a
s(i) = a
i = i + 1
Loop
Close #1
MsgBox s(2)
---------------------------------------------------------------------
the error it gives me is:
run time error "9"
"subscript out of range"
---------------------------------------------------------------------
already thanks, d-deamon
chibishmoo
10-18-2003, 07:59 AM
I dont have visual basic with me at the moment, but I think you may have to set the length of the array. The problem is, you are saving into the array at a place that doesnt exist, you are going out of its range. Try typing
-----------------------------------------------------
Dim a As String
Dim s(20) As String
Dim i As Integer
i = 1
Open "e:\test.txt" For Input As #1
Do Until EOF(1)
Input #1, a
s(i) = a
i = i + 1
Loop
Close #1
MsgBox s(2)
-----------------------------------------------------
If that works, you are going to need to find the code to resize the size of the array during run time, in case the amount of lines in the text file is greater than 20.
Remember, I think you cannot use the first place in the array because its used for something else. I think this would be s(0). Either that, or its the last one.
d-deamon
10-18-2003, 08:04 AM
wow thanx got it working now
maybe if i just do a loop first counting the lines:
dim t as integer
open "e:\test.txt" for input as #1
do until eof(1)
t = t + 1
loop
close #1
dim s(t) as string
blablalba.......
.....
....
wouldnt that work ? (and ps, how can i get those [code] tags in my text, is it just ?)
thanks, d-deamon
nope that wouldnt work =]
Robse
10-18-2003, 08:13 AM
Yes that would work. But then you'd have to open and input the file
again...which is unnessecary. See Loquin's Standards and Practices Tutorial (http://www.visualbasicforum.com/t68153.html) for good pointers on how to use arrays in VB.
Here's another example of how you could do it:
Dim a As String
Dim s() As String
Dim i As Integer
'Initialize array
ReDim s(0)
'Read file's contents into array
Open "c:\test.txt" For Input As #1
Do Until EOF(1)
Input #1, a
s(i) = a
i = i + 1
'Resize the array
ReDim Preserve s(i)
Loop
Close #1
'Remove last (unused) part of array:
ReDim Preserve s(UBound(s) - 1)
'Output array
For i = LBound(s) To UBound(s)
Debug.Print s(i)
Next i
For the code formatting question see my sig ;)
d-deamon
10-18-2003, 08:16 AM
wow thanks man. i get the point of the code.
you redim s(nextnumber) just before it gets filled, man, didnt think about that (didnt know the code either, but hey :P)
thanks for your help, both of you.
Many thankfull greets, d-deamon
and i wouldnt use the debug, because the file has over 5000 lines ;), worth the try clicking em away in 1 minute, but it was a program, not a game =]
d-deamon
10-18-2003, 08:21 AM
about the last part removal:
just use:
Dim a As String
Dim s() As String
Dim i As Integer
i = -1
'Initialize array
ReDim s(0)
'Read file's contents into array
Open "c:\test.txt" For Input As #1
Do Until EOF(1)
i = i + 1
'Resize the array
ReDim Preserve s(i)
Input #1, a
s(i) = a
Loop
Close #1
that should work also i think ^_^
Robse
10-18-2003, 08:31 AM
Yes of course, you're right. I sometimes get a little confused when
using Redim in loops... :huh: :o :D