CloseFile.htm 2.1 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>CloseFile filehandle</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. filehandle = variable defined with the WriteFile or OpenFile command
  13. </td>
  14. </tr>
  15. </table>
  16. <h1>Description</h1>
  17. <table>
  18. <tr>
  19. <td>
  20. Use this command to close a file previously opened. You should always close a file as soon as you have finished reading or writing to it.
  21. </td>
  22. </tr>
  23. </table>
  24. <h1><a href=../2d_examples/CloseFile.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=CloseFile&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  82. </html>