55 lines
3.5 KiB
HTML
55 lines
3.5 KiB
HTML
<html><head><title>Command: WriteFile </title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'><link rel='stylesheet' href='../css/commands.css' type='text/css'></head><body><span class='Command'> WriteFile (filename$) </span></p><span class='header'>Definition:</span> <br><br><table width='100%' border='0' cellspacing='2' cellpadding='2' align='center'><tr><td>Opens a file on disk for writing operations.</td></tr></table><span class='header'><br>Parameter Description:</span> <br><br><table width='100%' border='0' cellspacing='2' cellpadding='2' align='center'><tr><td>filename$ = any valid path and filename. The returned value is the filehandle which is an integer value.</td></tr></table><p class='header'>Command Description: <br><br><table width='100%' border='0' cellspacing='2' cellpadding='2' align='center'><tr><td>This command opens the designated filename and prepares it to be written to. Use this to write your own configuration file, save game data, etc. also useful for saving custom types to files. The filehandle that is returned is
|
|
an integer value that the operating system uses to identify which file is to be written to and must be passed to the functions such as WriteInt(). If the file could not be opened then the filehandle is Zero.<br>
|
|
<br>
|
|
See also:<br>
|
|
<a href='ReadByte.htm'>ReadByte</a>, <a href='ReadShort.htm'>ReadShort</a>, <a href='ReadInt.htm'>ReadInt</a>, <a href='ReadFloat.htm'>ReadFloat</a>, <a href='ReadString$.htm'>ReadString$</a>, <a href='ReadLine$.htm'>ReadLine$</a>, <a href='WriteByte.htm'>WriteByte</a>, <a href='WriteShort.htm'>WriteShort</a>, <a href='WriteInt.htm'>WriteInt</a>, <a href='WriteFloat.htm'>WriteFloat</a>, <a href='WriteShort.htm'>WriteShort</a>, <a href='WriteString.htm'>WriteString</a>,
|
|
<a href='WriteLine.htm'>WriteLine</a>, <a href='ReadFile.htm'>ReadFile</a>, <a href='WriteFile.htm'>WriteFile</a>, <a href='OpenFile.htm'>OpenFile</a>, <a href='CloseFile.htm'>CloseFile</a>, <a href='Eof.htm'>Eof</a>, <a href='FileType.htm'>FileType</a>, <a href='FilePos.htm'>FilePos</a>, <a href='SeekFile.htm'>SeekFile</a></td></tr></table><p class='header'>Example: <br><br><table width='100%' border='0' cellspacing='2' cellpadding='2' align='center'><tr><td>; Reading and writing custom types to files using ReadFile, WriteFile and
|
|
CloseFile<br>
|
|
<br>
|
|
; Initialise some variables for the example<br>
|
|
Type HighScore<br>
|
|
Field Name$<br>
|
|
Field Score%<br>
|
|
Field Level%<br>
|
|
End Type<br>
|
|
<br>
|
|
Best.HighScore = New HighScore<br>
|
|
Best\Name = "Mark"<br>
|
|
Best\Score = 11657<br>
|
|
Best\Level = 34<br>
|
|
<br>
|
|
; Open a file to write to<br>
|
|
fileout = WriteFile("mydata.dat")<br>
|
|
<br>
|
|
; Write the information to the file<br>
|
|
WriteString( fileout, Best\Name )<br>
|
|
WriteInt( fileout, Best\Score )<br>
|
|
WriteByte( fileout, Best\Level )<br>
|
|
<br>
|
|
; Close the file<br>
|
|
CloseFile( fileout )<br>
|
|
<br>
|
|
; Open the file to Read<br>
|
|
filein = ReadFile("mydata.dat")<br>
|
|
<br>
|
|
; Lets read the Greatest score from the file<br>
|
|
Greatest.HighScore = New HighScore<br>
|
|
Greatest\Name$ = ReadString$( filein )<br>
|
|
Greatest\Score = ReadInt( filein )<br>
|
|
Greatest\Level = ReadByte( filein )<br>
|
|
<br>
|
|
; Close the file once reading is finished<br>
|
|
CloseFile( fileout )<br>
|
|
<br>
|
|
Print "High score record read from - mydata.dat "<br>
|
|
Print<br>
|
|
Write "Name = "<br>
|
|
Print Greatest\Name<br>
|
|
Write "Score = "<br>
|
|
Print Greatest\Score<br>
|
|
Write "Level = "<br>
|
|
Print Greatest\Level<br>
|
|
<br>
|
|
WaitKey()<br></td></tr></table><p><b><a target="_top" href="../index.htm">Index</a></b></p></body>
|
|
</html>
|