| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <html>
- <head>
- <title>Blitz3D Docs</title>
- <link rel=stylesheet href=../css/commands.css type=text/css>
- </head>
- <body>
- <h1>WriteByte (filehandle/stream, mybyte)</h1>
- <h1>Parameters</h1>
- <table>
- <tr>
- <td>
- filehandle/stream = a valid variable set with the OpenFile, ReadFile command, or OpenTCPStream (v1.52+)
<br />
- mybyte = can be an Integer or a floating point number, but only values between 0 and 255 will be stored faithfully.
- </td>
- </tr>
- </table>
- <h1>Description</h1>
- <table>
- <tr>
- <td>
- 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 />
-
<br />
- Advanced notes:
<br />
-
<br />
- 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 />
-
<br />
- Streams can only be used in Blitz Basic v1.52 or greater.
- </td>
- </tr>
- </table>
- <h1><a href=../2d_examples/WriteByte.bb>Example</a></h1>
- <table>
- <tr>
- <td>
- ; Reading and Writing a file using ReadByte and WriteByte functions
<br />
-
<br />
- ; Initialise some variables for the example
<br />
- Byte1% = 10 ; store 10
<br />
- Byte2% = 100 ; store 100
<br />
- Byte3% = 255 ; store 255 ( the maximum value that can be stored in a Byte )
<br />
- Byte4% = 256 ; try to store 256 this will end up as 0 ( i.e. 256 - 256 = 0 )
<br />
- Byte5% = 257 ; try to store 257 this will end up as 1 ( i.e. 257 - 256 = 1 )
<br />
- Byte6% = -1 ; try to store -1 this will end up as 255 ( i.e. 256 -1 = 255 )
<br />
- Byte7% = -2 ; try to store 256 this will end up as 254 ( i.e. 256 - 2 = 254 )
<br />
- Byte8% = Asc("A") ; Store the ASCII value for the Character "A" ( i.e. 65 )
<br />
-
<br />
- ; Open a file to write to
<br />
- fileout = WriteFile("mydata.dat ")
<br />
-
<br />
- ; Write the information to the file
<br />
- WriteByte( fileout, Byte1 )
<br />
- WriteByte( fileout, Byte2 )
<br />
- WriteByte( fileout, Byte3 )
<br />
- WriteByte( fileout, Byte4 )
<br />
- WriteByte( fileout, Byte5 )
<br />
- WriteByte( fileout, Byte6 )
<br />
- WriteByte( fileout, Byte7 )
<br />
- WriteByte( fileout, Byte8 )
<br />
-
<br />
- ; Close the file
<br />
- CloseFile( fileout )
<br />
-
<br />
- ; Open the file to Read
<br />
- filein = ReadFile("mydata.dat")
<br />
-
<br />
- Read1 = ReadByte( filein )
<br />
- Read2 = ReadByte( filein )
<br />
- Read3 = ReadByte( filein )
<br />
- Read4 = ReadByte( filein )
<br />
- Read5 = ReadByte( filein )
<br />
- Read6 = ReadByte( filein )
<br />
- Read7 = ReadByte( filein )
<br />
- Read8 = ReadByte( filein )
<br />
-
<br />
- ; Close the file once reading is finished
<br />
- CloseFile( filein )
<br />
-
<br />
- Print "Written - Read"
<br />
- Write Byte1 + " - " : Print Read1
<br />
- Write Byte2 + " - " : Print Read2
<br />
- Write Byte3 + " - " : Print Read3
<br />
- Write Byte4 + " - " : Print Read4
<br />
- Write Byte5 + " - " : Print Read5
<br />
- Write Byte6 + " - " : Print Read6
<br />
- Write Byte7 + " - " : Print Read7
<br />
- Write Byte8 + " - " : Print Chr$( Read8 )
<br />
-
<br />
- WaitKey()
<br />
- </td>
- </tr>
- </table>
- <br>
- <a target=_top href=../index.htm>Index</a><br>
- <br>
- 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>
- </html>
|