Blitz3D+ Command Reference

ReadString$ ( stream )

Parameters

stream - a file stream opened for reading, or a TCP stream

Description

Reads a string written with WriteString from a file or stream.

WriteString stores a string as a 4-byte length followed by the characters themselves, and ReadString expects exactly that layout. Strings of any length round-trip perfectly, including empty ones and text containing line breaks - which is what makes the pair better than WriteLine/ReadLine for binary save data.

Because of the length prefix this is strictly for binary files and streams: pointing ReadString at a plain text file makes it treat the first four characters as a length, with junk results - use ReadLine for text. A wildly wrong length (from reading the wrong kind of file, or a desynced stream) can also stop the program with a runtime error.

Reading past the end of the file returns "".

See also: WriteString, ReadLine, ReadInt, Eof.

Example

; ReadString Example
; ------------------

; Save two strings so there is something to read
file=WriteFile("demo_savegame.dat")
WriteString file,"Robo"
WriteString file,"The Caves of Doom"
CloseFile file

; ReadString$ reads the 4-byte length written by WriteString,
; then exactly that many characters - any length round-trips
file=ReadFile("demo_savegame.dat")
name$=ReadString$(file)
level$=ReadString$(file)
CloseFile file

Print "Player: "+name$
Print "Level : "+level$

; Clean up the demo file
DeleteFile "demo_savegame.dat"
Print "Deleted demo_savegame.dat"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index