ReadBytes ( bank,file,offset,count )

Parameters

bank - bank to read into

file - file stream or TCP stream to read from

offset - position in the bank where the first byte lands

count - number of bytes to read

Description

Reads a block of bytes from a file or stream into a bank.

This is the fast, bulk way to load binary data: one call replaces a loop of thousands of ReadByte calls. A common pattern loads an entire file in one go - b=CreateBank(FileSize(f$)) then ReadBytes b,file,0,BankSize(b) - after which you pick it apart with the Peek commands.

Returns how many bytes were actually read, which can be less than count if the file ran out or the stream closed - always worth checking on network streams.

Note that offset and count describe where the data goes in the bank, not a position in the file, and that range must fit inside the bank or a runtime error is raised - size the bank first with CreateBank or ResizeBank.

See also: WriteBytes, CreateBank, FileSize, PeekByte.

Example

; ReadBytes Example
; -----------------

; Build a 16-byte file with a recognisable pattern in it
bank=CreateBank(16)
For i=0 To 15
    PokeByte bank,i,i*16
Next
file=WriteFile("demo_block.dat")
WriteBytes bank,file,0,16
CloseFile file
FreeBank bank
Print "Created demo_block.dat ("+FileSize("demo_block.dat")+" bytes)"

; ReadBytes copies a block of file bytes straight into a bank
bank=CreateBank(16)
file=ReadFile("demo_block.dat")
ReadBytes bank,file,0,16
CloseFile file

; Hex dump the bank to show what arrived
dump$=""
For i=0 To 15
    dump$=dump$+Right$(Hex$(PeekByte(bank,i)),2)+" "
Next
Print "Bank now holds: "+dump$
FreeBank bank

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

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

End

Index