
-----------------------------------
Selfi
Wed May 26, 2004 3:25 pm

reading files
-----------------------------------
im trying to read in a file using get, go to a random line, and put everything on that line in a variable, but ive had no luck so far. everytime i try to do this the word gets all cut off. its for a hangman game, any help?

-----------------------------------
GB
Wed May 26, 2004 3:31 pm


-----------------------------------
You have to declare different variables for them. Post the script, and then I'll help out.

-----------------------------------
Selfi
Wed May 26, 2004 3:32 pm


-----------------------------------
what do you mean i need to declare different variables for them? im only trying to read in one line

-----------------------------------
Selfi
Wed May 26, 2004 3:38 pm


-----------------------------------
all im trying to do is read in a random line, thats it

-----------------------------------
guruguru
Wed May 26, 2004 4:19 pm


-----------------------------------
Go through all the lines until you get to your line, then that is the last thing your string variables is assigned to. Kinda unefficient though me thinks  :x .


var myFile : string := "myFile.txt"
var fileNumber : int 
var randString : string
var randomLine : int

randint (randomLine, 0, 100)

open : fileNumber, myFile, get

for i : 1 .. randomLine
    exit when eof
    get : randString *
end for

close : fileNumber

put randString


-----------------------------------
Selfi
Wed May 26, 2004 4:25 pm


-----------------------------------
that dosent exactly work for me, here, ill post the file and my code

-----------------------------------
Selfi
Wed May 26, 2004 4:26 pm


-----------------------------------
you can try your code with it too, it still won't produce a full line  :(

-----------------------------------
guruguru
Wed May 26, 2004 4:35 pm


-----------------------------------
I'll tell ya why. Seek searches per byte. A byte is one letter. Hence,  you are searching between 1 and 85 bytest into the file. Seek is hard to use. My code is faulty... working on it right now...

-----------------------------------
Selfi
Wed May 26, 2004 4:39 pm


-----------------------------------
ah, that would explain it then. it said "fileposition" in the help file, so i just assumed fileposition would be the line

-----------------------------------
Selfi
Wed May 26, 2004 4:42 pm


-----------------------------------
ahh got it, thanks alot for your help  :) 


var myFile : string := "music.hman"
var fileNumber : int
var randString : string
var randomLine : int

randint (randomLine, 0, 84)

open : fileNumber, myFile, get
for i : 1 .. randomLine
    exit when eof (fileNumber)
    get : fileNumber, randString : *

end for
close : fileNumber
put randString


-----------------------------------
guruguru
Wed May 26, 2004 6:23 pm


-----------------------------------
No problem. Glad you got it working! Just a curiosity... there are less than 50 lines in the file you provided, yet you are making a variable that adheres to a file of up to 84 lines!? Is that a mistake or is your real/renewed file longer?

And what is this to be? Are you making a Windows Media Player type thing to play songs from data file? That would be pretty cool...
