 |
 |

10-18-2003, 07:51 AM
|
|
Newcomer
|
|
Join Date: Oct 2003
Posts: 14
|
|
*help*Loading text in an array
|
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
|
|

10-18-2003, 07:59 AM
|
|
Junior Contributor
|
|
Join Date: Jun 2003
Posts: 225
|
|
|
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.
|
|

10-18-2003, 08:04 AM
|
|
Newcomer
|
|
Join Date: Oct 2003
Posts: 14
|
|
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
Edit: nope that wouldnt work =]
|
Last edited by d-deamon; 10-18-2003 at 08:12 AM.
|

10-18-2003, 08:13 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
|
|
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 for good pointers on how to use arrays in VB.
Here's another example of how you could do it:
Code:
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 
|
|

10-18-2003, 08:16 AM
|
|
Newcomer
|
|
Join Date: Oct 2003
Posts: 14
|
|
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
Edit: 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 =]
|
|

10-18-2003, 08:21 AM
|
|
Newcomer
|
|
Join Date: Oct 2003
Posts: 14
|
|
about the last part removal:
just use:
Code:
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 ^_^
|
|

10-18-2003, 08:31 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
|
|
Yes of course, you're right. I sometimes get a little confused when
using Redim in loops... 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|