|
-
Aug 17th, 2001, 01:14 AM
#1
Thread Starter
Junior Member
Putting text file entries into an array
Hey all!
OK, what I want to do is put a whole bunch of names into an array from a text file. The names are already in correct order, all I want to do is get entry #1 into label #1, entry #2 into label #2 etc. How do I do this?
This is what I've got, but I know it doesn't work.
Code:
Private Sub Form_Load()
Dim Name as String, i as String
Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
For i = 1 To 52
Input #1, Name
lblPlayer(i).Caption = Name
Name = ""
Next i
End Sub
Thanks in advance for any help you can offer.
-
Aug 17th, 2001, 01:19 AM
#2
How is the text file set up? Comma delimited, each player has a line?
-
Aug 17th, 2001, 01:21 AM
#3
Try:
Code:
Private Sub Form_Load()
Dim Name as String
dim i as byte
Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
For i = 1 To 52
Input #1, Name
lblPlayer(i).Caption = Name
Next i
close #1
End Sub
-
Aug 17th, 2001, 01:22 AM
#4
Thread Starter
Junior Member
Only player's names are in the text file, and there is one per line, like this:
Leon Davis
Damien Adkins
Mark Richardson
etc.
-
Aug 17th, 2001, 01:28 AM
#5
This was what I meant to post:
Code:
Private Sub Form_Load()
Dim Name as String
dim i as byte
Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
For i = 1 To 52
Line Input #1, Name
lblPlayer(i).Caption = Name
Next i
close #1
End Sub
-
Aug 17th, 2001, 01:32 AM
#6
Thread Starter
Junior Member
No good... 'Run Time Error 54, Bad File Mode'
-
Aug 17th, 2001, 01:32 AM
#7
Thread Starter
Junior Member
No good... 'Run Time Error 54, Bad File Mode'
And, it is completely clearing all text from the Players.txt file every time.
-
Aug 17th, 2001, 01:38 AM
#8
Sorry I hadn't seen that, you should be using
Open "c:\lenny\collingwood\files\Players.txt" For Input As #1
-
Aug 17th, 2001, 02:00 AM
#9
Thread Starter
Junior Member
No, I need the text file to stay the same, and the program to read from the text file and put the text file information into an array.
Therefore, I know that this is right:
Open "c:\lenny\collingwood\files\Players.txt" For Output As #1
But, what should I use here:
For i = 1 To 52
Line Input #1, Name
lblPlayer(i).Caption = Name
Next i
instead of Line Input, in order to READ the file, not WRITE to it?
-
Aug 17th, 2001, 02:03 AM
#10
Well thats why you need to use "INPUT" instead of "OUTPUT"
INPUT allows you to read from a file, while OUTPUT allows you to write to the file.
-
Aug 17th, 2001, 02:05 AM
#11
Thread Starter
Junior Member
Oh... okay then. I was certain it was the other way around. Thanks
-
Aug 17th, 2001, 02:54 AM
#12
Retired VBF Adm1nistrator
The best way, in my humble opinion, to load an entire file, line by line, into an array :
VB Code:
Dim myArray() As String
Open "c:\autoexec.bat" For Binary As #1
myArray = Split(Input(LOF(1), 1), vbCrLf)
Close #1
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|