Home
Products
Community
Manuals
Contact
Login or Signup

Blitz3D Docs -> 2D - Category -> File -> ReadFile

ReadFile (filename$)

Parameters:

filename$ = any valid path and filename. The returned value is the filehandle which is an integer value.

Description:

This command opens the designated filename and prepares it to be read from. Use this to read back your own configuration file, save game data, etc. also useful for reading custom types from a files. The filehandle that is returned is an integer value that the operating system uses to identify which file is to be read from and must be passed to the functions such as ReadInt(). If the file could not be opened, for instance, if it does not exists, then the filehandle is Zero.

Example:

; Reading and writing custom types to files using ReadFile, WriteFile and  CloseFile 

; Initialise some variables for the example
Type HighScore
Field Name$
Field Score%
Field Level%
End Type

Best.HighScore = New HighScore
BestName = "Mark"
BestScore = 11657
BestLevel = 34

; Open a file to write to
fileout = WriteFile("mydata.dat")

; Write the information to the file
WriteString( fileout, BestName )
WriteInt( fileout, BestScore )
WriteByte( fileout, BestLevel )

; Close the file
CloseFile( fileout )

; Open the file to Read
filein = ReadFile("mydata.dat")

; Lets read the Greatest score from the file
Greatest.HighScore = New HighScore
GreatestName$ = ReadString$( filein )
GreatestScore = ReadInt( filein )
GreatestLevel = ReadByte( filein )

; Close the file once reading is finished
CloseFile( filein )

Print "High score record read from - mydata.dat "
Print
Write "Name = "
Print GreatestName
Write "Score = "
Print GreatestScore
Write "Level = "
Print GreatestLevel

WaitKey()

Comments

big10p(Posted 1+ years ago)
Alternatively, the same example as above which actually uses the HighScore type: :)

; Reading and writing custom types to files using ReadFile, WriteFile and CloseFile 

; Initialise some variables for the example
Type HighScore
Field Name$
Field Score%
Field Level%
End Type

Best.HighScore = New HighScore
Best\Name = "Mark"
Best\Score = 11657
Best\Level = 34

; Open a file to write to
fileout = WriteFile("mydata.dat")

; Write the information to the file
WriteString( fileout, Best\Name )
WriteInt( fileout, Best\Score )
WriteByte( fileout, Best\Level )

; Close the file
CloseFile( fileout )

; Open the file to Read
filein = ReadFile("mydata.dat")

; Lets read the Greatest score from the file
Greatest.HighScore = New HighScore
Greatest\Name$ = ReadString$( filein )
Greatest\Score = ReadInt( filein )
Greatest\Level = ReadByte( filein )

; Close the file once reading is finished
CloseFile( filein )

Print "High score record read from - mydata.dat "
Print
Write "Name = "
Print Greatest\Name
Write "Score = "
Print Greatest\Score
Write "Level = "
Print Greatest\Level

WaitKey() 



Lykaestria(Posted 1+ years ago)
The latter one actually works better... the first example just prints 0 in place of the name on my blitz3D. Win XP Pro bug? :-/


mv333(Posted 1+ years ago)
I get the same results as Lykaestria. Win7 bug?


Yasha(Posted 1+ years ago)
Nah, the first example just fails to declare BestName as a string so the value is converted to its int value - 0 (in fact, the example is a bit confused... why is there even a highscore type if it's not going to use it?).


Blitz3D Manual Forum

BlitzPlus Equivalent Command