WriteString/ReadString
BlitzMax Forums/BlitzMax Beginners Area/WriteString/ReadString
| ||
| Can someone please show me how to use the Read and WriteString commands properly? I haven't figured out how to use them in BlitzMAX. |
| ||
Strict
Local file:TStream
file = WriteFile("file.txt")
WriteString(file,"Hello")
CloseFile file
file = ReadFile("file.txt")
Print ReadString(file,5)
CloseFile file
Personally I find it more convenient to use ReadLine/WriteLine. |
| ||
If I write "HelloWorld" into the file and read it like thisReadString(file,5) ReadString(file,5)does the the second one read from where the first one left off? |
| ||
| I'd imagine so. Try it. You're not going to start World War 3 by trying stuff out. :) |
| ||
| Unless you're trying out nuclear weapons. In that case, ask first. |
| ||
| Nuke the ReadString from orbit. It's the only way to be sure. |
| ||
Quick and dirty but this is how I've done it for loading in high scores in a game I'm working on. Note it'll also make sure the file exists before it tries to load it else set up some default data.
Global HighName:String[9+1], HighScore:Int[9+1]
Pointer = OpenFile("highscore.txt") ' ** Check if there is valid high score data saved
If Not Pointer
' No HiScore.dat on disk so game is starting for first time. Load default High Scores
For T = 1 To 9
HighScore[T]=100000-T*10000
Next
HighName[1] = "Tony Black "
HighName[2] = "Simon Woods "
HighName[3] = "Chris Randals"
HighName[4] = "Andrew Burton"
HighName[5] = "Lee Scottow "
HighName[6] = "Mick Green "
HighName[7] = "Mathew Buttle"
HighName[8] = "Louis Denton "
HighName[9] = "Steve Denton "
Else ' ** There is a HiScore.dat file present so load it.
For T=1 To 9
HighName[T]=ReadLine$(Pointer)
HighScore[T]=Int(ReadLine(Pointer))
Next
CloseFile Pointer
EndIf
And to save the file when the program exits.
Pointer=WriteFile("highscore.txt")
For T=1 To 9
WriteLine Pointer,HighName[T]
WriteLine Pointer,HighScore[T]
Next
CloseFile Pointer
|
| ||
| You really should encrypt that to stop people cheating. Even basic XOR encryption is better than leaving high score data wide open to abuse. |
| ||
| It's ripped code from a game I'm in progress with. Just making sure it works for now. Be worth encrypting it if I think the games good enough to pass around once I've finished it. |