Blitz3D+ Command Reference

ReadFile ( filename$ )

Parameters

filename$ - path and name of the file to open

Description

Opens an existing file for reading.

Returns a file stream handle, or 0 if the file could not be opened (usually because it does not exist - always check before reading a save file that might not be there yet).

Feed the handle to the read commands that match how the file was written: ReadLine for plain text, ReadByte/ReadShort/ReadInt/ReadFloat/ReadString for binary data written by their Write counterparts, or ReadBytes to pull a block straight into a bank. Use Eof to spot the end of the file, and CloseFile when you are done.

The same read commands also work on TCP streams, so code that parses a file can often parse a network stream unchanged.

See also: WriteFile, OpenFile, CloseFile, ReadLine, Eof.

Example

; ReadFile Example
; ----------------

; Create a save-game file so there is something to read
file=WriteFile("demo_savegame.dat")
WriteString file,"Robo"
WriteInt file,11657
CloseFile file

; ReadFile opens an existing file for reading.
; It returns 0 if the file cannot be opened - always check!
file=ReadFile("no_such_file.dat")
If file=0 Then Print "no_such_file.dat could not be opened (handle = 0)"

; Open the real save and read it back in the order it was written
file=ReadFile("demo_savegame.dat")
name$=ReadString$(file)
score=ReadInt(file)
CloseFile file

Print "Loaded save: name="+name$+", score="+score

; 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