Computer Science Canada

reading files

Author:  Selfi [ Wed May 26, 2004 3:25 pm ]
Post subject:  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?

Author:  GB [ Wed May 26, 2004 3:31 pm ]
Post subject: 

You have to declare different variables for them. Post the script, and then I'll help out.

Author:  Selfi [ Wed May 26, 2004 3:32 pm ]
Post subject: 

what do you mean i need to declare different variables for them? im only trying to read in one line

Author:  Selfi [ Wed May 26, 2004 3:38 pm ]
Post subject: 

all im trying to do is read in a random line, thats it

Author:  guruguru [ Wed May 26, 2004 4:19 pm ]
Post subject: 

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 Mad .

code:

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

Author:  Selfi [ Wed May 26, 2004 4:25 pm ]
Post subject: 

that dosent exactly work for me, here, ill post the file and my code

Author:  Selfi [ Wed May 26, 2004 4:26 pm ]
Post subject: 

you can try your code with it too, it still won't produce a full line Sad

Author:  guruguru [ Wed May 26, 2004 4:35 pm ]
Post subject: 

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...

Author:  Selfi [ Wed May 26, 2004 4:39 pm ]
Post subject: 

ah, that would explain it then. it said "fileposition" in the help file, so i just assumed fileposition would be the line

Author:  Selfi [ Wed May 26, 2004 4:42 pm ]
Post subject: 

ahh got it, thanks alot for your help Smile

code:

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

Author:  guruguru [ Wed May 26, 2004 6:23 pm ]
Post subject: 

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...


: