WriteFile.htm 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <html>
  2. <head>
  3. <title>Blitz3D Docs</title>
  4. <link rel=stylesheet href=../css/commands.css type=text/css>
  5. </head>
  6. <body>
  7. <h1>WriteFile (filename$)</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. filename$ = any valid path and filename. The returned value is the filehandle which is an integer value.
  13. </td>
  14. </tr>
  15. </table>
  16. <h1>Description</h1>
  17. <table>
  18. <tr>
  19. <td>
  20. 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.
  21. </td>
  22. </tr>
  23. </table>
  24. <h1><a href=../2d_examples/WriteFile.bb>Example</a></h1>
  25. <table>
  26. <tr>
  27. <td>
  28. ; Reading and writing custom types to files using ReadFile, WriteFile and CloseFile <br />
  29. <br />
  30. ; Initialise some variables for the example <br />
  31. Type HighScore <br />
  32. Field Name$ <br />
  33. Field Score% <br />
  34. Field Level% <br />
  35. End Type <br />
  36. <br />
  37. Best.HighScore = New HighScore <br />
  38. BestName = "Mark" <br />
  39. BestScore = 11657 <br />
  40. BestLevel = 34 <br />
  41. <br />
  42. ; Open a file to write to <br />
  43. fileout = WriteFile("mydata.dat") <br />
  44. <br />
  45. ; Write the information to the file <br />
  46. WriteString( fileout, BestName ) <br />
  47. WriteInt( fileout, BestScore ) <br />
  48. WriteByte( fileout, BestLevel ) <br />
  49. <br />
  50. ; Close the file <br />
  51. CloseFile( fileout ) <br />
  52. <br />
  53. ; Open the file to Read <br />
  54. filein = ReadFile("mydata.dat") <br />
  55. <br />
  56. ; Lets read the Greatest score from the file <br />
  57. Greatest.HighScore = New HighScore <br />
  58. GreatestName$ = ReadString$( filein ) <br />
  59. GreatestScore = ReadInt( filein ) <br />
  60. GreatestLevel = ReadByte( filein ) <br />
  61. <br />
  62. ; Close the file once reading is finished <br />
  63. CloseFile( filein ) <br />
  64. <br />
  65. Print "High score record read from - mydata.dat " <br />
  66. Print <br />
  67. Write "Name = " <br />
  68. Print GreatestName <br />
  69. Write "Score = " <br />
  70. Print GreatestScore <br />
  71. Write "Level = " <br />
  72. Print GreatestLevel <br />
  73. <br />
  74. WaitKey()
  75. </td>
  76. </tr>
  77. </table>
  78. <br>
  79. <a target=_top href=../index.htm>Index</a><br>
  80. <br>
  81. Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=WriteFile&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  82. </html>