Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help in figuring out how to read from a text file 1

Status
Not open for further replies.

bigwilly

Technical User
Nov 23, 2000
2
US
I need help in figuring out how to read from a text file. Two types of text files in particular. Just a standard word list, and also a comma delimited or seperated word list. I am making a hangman game and I want a random word to be generated using a text file. Any help that anyone could offer would be greatly appreciated. Thank You For Your Time.

Sincerely,
Billy Willis
 
Try loading all of the words at the beginning and storing them in an array.


Open "textfile.txt" For Input As #1
For a = 1 To wordmax
Input #1, word(a)
Next
Close #1

newword = Int(Rnd(wordmax))
Print word(newword)


That should show a random word from a textfile that has them stored in it.
 
For a standard word list (with a new line for each word) I wrote this function to retrieve and return the word on the Nth line. The line wanted is passes into the function as an Integer. You could modify it easily to pull out all words:


Public Function GetLink(Field As Integer) As String
Dim Data As String
Dim i As Integer
Open "c:\linker.txt" For Input As #1 ' Open file for input.
For i = 1 To (Field) ' Loop until correct field is reached.
If EOF(1) Then ' Checks that field expected exists.
Result.Text = "Linker File Not As Expected"
Close #1
Exit Function
End If
Input #1, Data ' Read data into return variable.
Next
Close #1 ' Close file.
GetLink = Data ' Pass back data.
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top