Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 reading files
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Selfi




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
GB




PostPosted: Wed May 26, 2004 3:31 pm   Post subject: (No subject)

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




PostPosted: Wed May 26, 2004 3:32 pm   Post subject: (No subject)

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




PostPosted: Wed May 26, 2004 3:38 pm   Post subject: (No subject)

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




PostPosted: Wed May 26, 2004 4:19 pm   Post subject: (No 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
Selfi




PostPosted: Wed May 26, 2004 4:25 pm   Post subject: (No subject)

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


readline.zip
 Description:

Download
 Filename:  readline.zip
 Filesize:  750 Bytes
 Downloaded:  226 Time(s)

Selfi




PostPosted: Wed May 26, 2004 4:26 pm   Post subject: (No subject)

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




PostPosted: Wed May 26, 2004 4:35 pm   Post subject: (No 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...
Sponsor
Sponsor
Sponsor
sponsor
Selfi




PostPosted: Wed May 26, 2004 4:39 pm   Post subject: (No subject)

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




PostPosted: Wed May 26, 2004 4:42 pm   Post subject: (No 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
guruguru




PostPosted: Wed May 26, 2004 6:23 pm   Post subject: (No 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...
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: