Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > File I/O and Registry > *help*Loading text in an array


Reply
 
Thread Tools Display Modes
  #1  
Old 10-18-2003, 07:51 AM
d-deamon d-deamon is offline
Newcomer
 
Join Date: Oct 2003
Posts: 14
Question *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
Reply With Quote
  #2  
Old 10-18-2003, 07:59 AM
chibishmoo chibishmoo is offline
Junior Contributor
 
Join Date: Jun 2003
Posts: 225
Default

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.
Reply With Quote
  #3  
Old 10-18-2003, 08:04 AM
d-deamon d-deamon is offline
Newcomer
 
Join Date: Oct 2003
Posts: 14
Default

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.
Reply With Quote
  #4  
Old 10-18-2003, 08:13 AM
Robse's Avatar
Robse Robse is offline
Senior Contributor

* Expert *
 
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
Default

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
__________________
Posting Guidelines MSDN-VB API List Use [vb]...[/vb] tags for code!
Reply With Quote
  #5  
Old 10-18-2003, 08:16 AM
d-deamon d-deamon is offline
Newcomer
 
Join Date: Oct 2003
Posts: 14
Default

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 =]
Reply With Quote
  #6  
Old 10-18-2003, 08:21 AM
d-deamon d-deamon is offline
Newcomer
 
Join Date: Oct 2003
Posts: 14
Default

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 ^_^
Reply With Quote
  #7  
Old 10-18-2003, 08:31 AM
Robse's Avatar
Robse Robse is offline
Senior Contributor

* Expert *
 
Join Date: Sep 2002
Location: Karlsruhe, Germany
Posts: 1,319
Default

Yes of course, you're right. I sometimes get a little confused when
using Redim in loops...
__________________
Posting Guidelines MSDN-VB API List Use [vb]...[/vb] tags for code!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Standards and Practices loquin Tutors' Corner 10 07-28-2006 12:16 PM
Bullettin problem pckm_123 Word, PowerPoint, Outlook, and Other Office Products 1 12-03-2003 06:59 AM
Array and String Functions rhawke General 5 07-10-2003 02:33 AM
control array okiegrl General 9 07-13-2001 02:48 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->