Blitz3D+ Command Reference

WriteBytes ( bank,file,offset,count )

Parameters

bank - bank holding the data

file - file stream or TCP stream to write to

offset - position in the bank of the first byte to write

count - number of bytes to write

Description

Writes a block of bytes from a bank to a file or stream.

The bulk companion to ReadBytes: build data in a bank with the Poke commands (or load it from elsewhere), then push the whole block out in one call - WriteBytes b,file,0,BankSize(b) saves an entire bank. Far faster than writing values one at a time, and just as happy sending over a TCP stream as saving to disk.

Returns how many bytes were written. The offset and count describe the region of the bank being sent, and must fit inside it or a runtime error is raised.

See also: ReadBytes, CreateBank, PokeByte, WriteFile.

Example

; WriteBytes Example
; ------------------

; Fill a 16-byte bank with a recognisable pattern
bank=CreateBank(16)
For i=0 To 15
    PokeByte bank,i,i*16
Next

; WriteBytes copies a block of bank bytes straight into a file stream
file=WriteFile("demo_block.dat")
WriteBytes bank,file,0,16
CloseFile file
Print "Wrote 16 bank bytes - demo_block.dat is "+FileSize("demo_block.dat")+" bytes"

; Read the block back into a fresh bank to prove the round trip
FreeBank bank
bank=CreateBank(16)
file=ReadFile("demo_block.dat")
ReadBytes bank,file,0,16
CloseFile file

; Hex dump of the recovered bank
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