Blitz3D+ Command Reference

ReadByte ( stream )

Parameters

stream - a file stream opened for reading, or a TCP stream

Description

Reads one byte from a file or stream and returns it as a value from 0 to 255.

A byte is the smallest unit you can read - 8 bits, holding 0 to 255. The result is always unsigned: a -1 stored with WriteByte comes back as 255, because only the low 8 bits of the original number were kept. Since text characters are single bytes, ReadByte can also walk through a file character by character (Chr$ turns the value back into a character).

Reading past the end of the file is not an error - you just get 0 - so use Eof to know when to stop.

On a TCP stream the call waits until a byte arrives; check ReadAvail first if you cannot afford to wait.

See also: WriteByte, ReadShort, ReadInt, Eof, ReadAvail.

Example

; ReadByte Example
; ----------------

; Save a few bytes so there is something to read
file=WriteFile("demo_bytes.dat")
WriteByte file,65
WriteByte file,100
WriteByte file,255
CloseFile file

; ReadByte reads a single byte and always returns 0 to 255
file=ReadFile("demo_bytes.dat")
a=ReadByte(file)
b=ReadByte(file)
c=ReadByte(file)
CloseFile file

Print "Bytes read from demo_bytes.dat:"
Print "  "+a+"  (the ASCII code for "+Chr$(a)+")"
Print "  "+b
Print "  "+c+"  (the largest value a byte can hold)"

; Clean up the demo file
DeleteFile "demo_bytes.dat"
Print "Deleted demo_bytes.dat"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index