ReadByte.htm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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>ReadByte ( filehandle/stream )</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+)
  13. </td>
  14. </tr>
  15. </table>
  16. <h1>Description</h1>
  17. <table>
  18. <tr>
  19. <td>
  20. Once you've opened a disk file (or stream) for reading, use this command to read a single byte at a time from 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 read a file one character at a time. Reading beyond the end of file does not result in an error, but each value read will be zero. <br />
  21. <br />
  22. Advanced notes <br />
  23. <br />
  24. 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 />
  25. <br />
  26. Streams can only be used in Blitz Basic v1.52 or greater.
  27. </td>
  28. </tr>
  29. </table>
  30. <h1><a href=../2d_examples/ReadByte.bb>Example</a></h1>
  31. <table>
  32. <tr>
  33. <td>
  34. ; Reading and writing a file using ReadByte and WriteByte functions <br />
  35. <br />
  36. ; Initialise some variables for the example <br />
  37. Byte1% = 10 ; store 10 <br />
  38. Byte2% = 100 ; store 100 <br />
  39. Byte3% = 255 ; store 255 ( the maximum value that can be stored in a Byte) <br />
  40. Byte4% = 256 ; try to store 256 this will end up as 0 ( i.e. 256 - 256 = 0 ) <br />
  41. Byte5% = 257 ; try to store 257 this will end up as 1 ( i.e. 257 - 256 = 1 ) <br />
  42. Byte6% = -1 ; try to store -1 this will end up as 255 ( i.e. 256 -1 = 255 ) <br />
  43. Byte7% = -2 ; try to store 256 this will end up as 254 ( i.e. 256 - 2 = 254 ) <br />
  44. Byte8% = Asc("A") ; Store the ASCII value for the Character "A" ( i.e. 65 ) <br />
  45. <br />
  46. ; Open a file to write to <br />
  47. fileout = WriteFile("mydata.dat ") <br />
  48. <br />
  49. ; Write the information to the file <br />
  50. WriteByte( fileout, Byte1 ) <br />
  51. WriteByte( fileout, Byte2 ) <br />
  52. WriteByte( fileout, Byte3 ) <br />
  53. WriteByte( fileout, Byte4 ) <br />
  54. WriteByte( fileout, Byte5 ) <br />
  55. WriteByte( fileout, Byte6 ) <br />
  56. WriteByte( fileout, Byte7 ) <br />
  57. WriteByte( fileout, Byte8 ) <br />
  58. <br />
  59. ; Close the file <br />
  60. CloseFile( fileout ) <br />
  61. <br />
  62. ; Open the file to Read <br />
  63. filein = ReadFile("mydata.dat") <br />
  64. <br />
  65. Read1 = ReadByte( filein ) <br />
  66. Read2 = ReadByte( filein ) <br />
  67. Read3 = ReadByte( filein ) <br />
  68. Read4 = ReadByte( filein ) <br />
  69. Read5 = ReadByte( filein ) <br />
  70. Read6 = ReadByte( filein ) <br />
  71. Read7 = ReadByte( filein ) <br />
  72. Read8 = ReadByte( filein ) <br />
  73. <br />
  74. ; Close the file once reading is finished <br />
  75. CloseFile( filein ) <br />
  76. <br />
  77. Print "Written - Read" <br />
  78. Write Byte1 + " - " : Print Read1 <br />
  79. Write Byte2 + " - " : Print Read2 <br />
  80. Write Byte3 + " - " : Print Read3 <br />
  81. Write Byte4 + " - " : Print Read4 <br />
  82. Write Byte5 + " - " : Print Read5 <br />
  83. Write Byte6 + " - " : Print Read6 <br />
  84. Write Byte7 + " - " : Print Read7 <br />
  85. Write Byte8 + " - " : Print Chr$( Read8 ) <br />
  86. <br />
  87. WaitKey() <br />
  88. </td>
  89. </tr>
  90. </table>
  91. <br>
  92. <a target=_top href=../index.htm>Index</a><br>
  93. <br>
  94. Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=ReadByte&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  95. </html>