WriteByte.htm 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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>WriteByte (filehandle/stream, mybyte)</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. filehandle/stream = a valid variable set with the OpenFile, ReadFile command, or OpenTCPStream (v1.52+) <br />
  13. mybyte = can be an Integer or a floating point number, but only values between 0 and 255 will be stored faithfully.
  14. </td>
  15. </tr>
  16. </table>
  17. <h1>Description</h1>
  18. <table>
  19. <tr>
  20. <td>
  21. Once you've opened a disk file (or stream) for reading, use this command to write a single byte at a time to the file/stream. Note, a byte is an integer that can take the values 0..255 and occupies 8 bits of storage. Since characters are stored as byte values this function can be used to create a text file one character at a time. <br />
  22. <br />
  23. Advanced notes: <br />
  24. <br />
  25. The number that is stored by WriteByte is actually the least significant byte of an integer so negative numbers and numbers above 255 will still have a value between 0..255. Unless you understand how 32 bit integers are stored in 2's compliment notation this will seem strange but it is NOT a bug. <br />
  26. <br />
  27. Streams can only be used in Blitz Basic v1.52 or greater.
  28. </td>
  29. </tr>
  30. </table>
  31. <h1><a href=../2d_examples/WriteByte.bb>Example</a></h1>
  32. <table>
  33. <tr>
  34. <td>
  35. ; Reading and Writing a file using ReadByte and WriteByte functions <br />
  36. <br />
  37. ; Initialise some variables for the example <br />
  38. Byte1% = 10 ; store 10 <br />
  39. Byte2% = 100 ; store 100 <br />
  40. Byte3% = 255 ; store 255 ( the maximum value that can be stored in a Byte ) <br />
  41. Byte4% = 256 ; try to store 256 this will end up as 0 ( i.e. 256 - 256 = 0 ) <br />
  42. Byte5% = 257 ; try to store 257 this will end up as 1 ( i.e. 257 - 256 = 1 ) <br />
  43. Byte6% = -1 ; try to store -1 this will end up as 255 ( i.e. 256 -1 = 255 ) <br />
  44. Byte7% = -2 ; try to store 256 this will end up as 254 ( i.e. 256 - 2 = 254 ) <br />
  45. Byte8% = Asc("A") ; Store the ASCII value for the Character "A" ( i.e. 65 ) <br />
  46. <br />
  47. ; Open a file to write to <br />
  48. fileout = WriteFile("mydata.dat ") <br />
  49. <br />
  50. ; Write the information to the file <br />
  51. WriteByte( fileout, Byte1 ) <br />
  52. WriteByte( fileout, Byte2 ) <br />
  53. WriteByte( fileout, Byte3 ) <br />
  54. WriteByte( fileout, Byte4 ) <br />
  55. WriteByte( fileout, Byte5 ) <br />
  56. WriteByte( fileout, Byte6 ) <br />
  57. WriteByte( fileout, Byte7 ) <br />
  58. WriteByte( fileout, Byte8 ) <br />
  59. <br />
  60. ; Close the file <br />
  61. CloseFile( fileout ) <br />
  62. <br />
  63. ; Open the file to Read <br />
  64. filein = ReadFile("mydata.dat") <br />
  65. <br />
  66. Read1 = ReadByte( filein ) <br />
  67. Read2 = ReadByte( filein ) <br />
  68. Read3 = ReadByte( filein ) <br />
  69. Read4 = ReadByte( filein ) <br />
  70. Read5 = ReadByte( filein ) <br />
  71. Read6 = ReadByte( filein ) <br />
  72. Read7 = ReadByte( filein ) <br />
  73. Read8 = ReadByte( filein ) <br />
  74. <br />
  75. ; Close the file once reading is finished <br />
  76. CloseFile( filein ) <br />
  77. <br />
  78. Print "Written - Read" <br />
  79. Write Byte1 + " - " : Print Read1 <br />
  80. Write Byte2 + " - " : Print Read2 <br />
  81. Write Byte3 + " - " : Print Read3 <br />
  82. Write Byte4 + " - " : Print Read4 <br />
  83. Write Byte5 + " - " : Print Read5 <br />
  84. Write Byte6 + " - " : Print Read6 <br />
  85. Write Byte7 + " - " : Print Read7 <br />
  86. Write Byte8 + " - " : Print Chr$( Read8 ) <br />
  87. <br />
  88. WaitKey() <br />
  89. </td>
  90. </tr>
  91. </table>
  92. <br>
  93. <a target=_top href=../index.htm>Index</a><br>
  94. <br>
  95. Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=WriteByte&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  96. </html>