Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to read in an integer from an external file

Author
Message
Chinless Miracle
14
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 24th Mar 2012 13:42
Hi there trying to create a high score table and i'm having troubles getting the score values in. Please help


Thanks, and goodnight.
Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 24th Mar 2012 14:52
you always can use the
read string 1,s$ command,
and then use val() to convert from string to integer..

open to write 1,"Hello.txt"
write string 1,"score=10"
close file 1

open to read 1,"hello.txt"
read string 1,s$
close file 1
s$ = right$(s$,len(s$)-len("score="))
score = val(s$)
print "Score: "+str$(score)
wait key


more 3d models .x/.obj and more foramts here:
[href]https://www.turbosquid.com/Search/Index.cfm?keyword=gogetax1&x=0&y=0[href]
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 24th Mar 2012 16:12
I think I might write a tutorial on tokens because they can simplify the work load a lot.

open to write 1,"hello.txt"
write string 1,"James Brown=55"
close file 1

open to read 1,"hello.txt"
read string 1,s$
close file 1
print first token$( s$, "=" );" - ";val(next token$( "=" ))
wait key
The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 24th Mar 2012 20:55 Edited at: 24th Mar 2012 21:03
use the commands "read long" and "write long"

reading

open to read 1,"High Scores.txt"
for reading = 1 to 10
read string 1, name$(reading)
read long 1, score(reading)
next reading

writing

open to write 2,"High Scores.txt"
for write = 1 to 10
write string 2,name$(write)
write long 2,score(write)
next write
close file 2

delete any existing "High Scores.txt" file before you try this for the first time, otherwise "read long" will happily read the old string data and present you with garbage.

Also, note that you are no longer dealing with pure text files, you have entered into the scary world of binary files, so maybe "High Scores.bin" would be more a suitable name

Login to post a reply

Server time is: 2026-07-09 15:47:03
Your offset time is: 2026-07-09 15:47:03