Blitz3D+ Command Reference

WriteString stream,string$

Parameters

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

string$ - string to write

Description

Writes a string to a file or stream in binary form.

The string is stored as a 4-byte length followed by its characters - no terminator, no line break, no length limit. That layout means any string survives the trip intact, even one containing line breaks or other control characters, and an empty string is stored as just its zero length. ReadString reads it back.

Because of the length prefix the result is not a plain text file: open it in Notepad and you will see stray characters between the strings. When humans should be able to read or edit the file, use WriteLine instead.

See also: ReadString, WriteLine, WriteInt.

Example

; WriteString Example
; -------------------

; WriteString stores a string with a 4-byte length in front,
; so text of any length round-trips exactly
file=WriteFile("demo_savegame.dat")
WriteString file,"Robo"
WriteString file,"The Caves of Doom"
CloseFile file

Print "Wrote 2 strings - demo_savegame.dat is "+FileSize("demo_savegame.dat")+" bytes"

; Read them back in the same order
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