Blitz3D+ Command Reference

CloseFile file_stream

Parameters

file_stream - file handle returned by ReadFile, WriteFile or OpenFile

Description

Closes a file opened with ReadFile, WriteFile or OpenFile.

Closing a file flushes any writes still sitting in memory out to disk - until then a freshly written save may only partly exist. Make it a habit: open, read or write, close, as soon as you are done.

The handle is invalid after closing; using it again raises a runtime error. Files left open are closed when the program ends, but do not lean on that for anything you care about.

See also: OpenFile, ReadFile, WriteFile.

Example

; CloseFile Example
; -----------------

; Write a small save-game file
file=WriteFile("demo_savegame.dat")
WriteString file,"Robo"
WriteInt file,11657

; CloseFile flushes the data to disk and releases the file.
; Always close a file as soon as you have finished with it
CloseFile file
Print "Save written and closed"

; Because the file was closed, it is safe to reopen it for reading
file=ReadFile("demo_savegame.dat")
Print "Name : "+ReadString$(file)
Print "Score: "+ReadInt(file)
CloseFile file

; 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