Blitz3D+ Command Reference

WriteFile ( filename$ )

Parameters

filename$ - path and name of the file to create

Description

Creates a file and opens it for writing.

Returns a file stream handle for the write commands, or 0 if the file could not be created (bad path, or the file is locked). This is the command for saving: config files, high scores, save games, level data.

If a file with that name already exists its contents are wiped the moment WriteFile succeeds - there is no undo, so if the old version matters, CopyFile it to a backup first.

Write with WriteLine for plain text, or WriteByte/WriteShort/WriteInt/WriteFloat/WriteString for binary data, then CloseFile - closing flushes everything to disk, so a save is not safe until the file is closed.

See also: ReadFile, OpenFile, CloseFile, WriteLine, WriteInt.

Example

; WriteFile Example
; -----------------

; WriteFile creates a new file (or overwrites an old one) and
; returns a stream handle ready for writing
file=WriteFile("demo_savegame.dat")

; Save the player's progress
WriteString file,"Robo"
WriteInt file,11657
WriteFloat file,12.5
WriteFloat file,-3.25
CloseFile file
Print "Saved game to demo_savegame.dat ("+FileSize("demo_savegame.dat")+" bytes)"

; Load the save back to prove it worked
file=ReadFile("demo_savegame.dat")
name$=ReadString$(file)
score=ReadInt(file)
x#=ReadFloat(file)
y#=ReadFloat(file)
CloseFile file
Print "Loaded: "+name$+", score "+score+", position "+x+","+y

; 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