WRITE
Syntax
WRITE [ # Stream , ] Expression [ , Length ]
WRITE [ # Pointer , ] Expression [ , Length ]
Writes an expression to the stream Stream by using their binary representation.
If the stream is not specified, the standard output is used.
If Expression is a string, you can specify a Length that indicates the number of bytes to write. If no length is specified for a string, it is directly written to the stream just before the string data.
 |
The data be wrote to the stream by this instruction is the byte order of the stream.
|
If you specify a Pointer instead of a Stream, then data will be written directly to
the memory address specified by the pointer.
 |
If you try to write at a forbidden memory address, you will get an error. The interpreter won't crash.
|
Examples
... ' The following bytes will be written to the stream (hexadecimal notation) :
WRITE #hfile, "123456789" ' 09 31 32 33 34 35 36 37 38 39
WRITE #hfile, "123456789", 0 ' 09 31 32 33 34 35 36 37 38 39
WRITE #hfile, "123456789", 4 ' 31 32 33 34
WRITE #hfile, "123456789", 11 ' 31 32 33 34 35 36 37 38 39 00 00
WRITE #hfile, "123456789", -2 ' Nothing will be written to the stream.
...
This example shows how we can write a binary file. It then READ's the file created so we can display the file content.
PUBLIC SUB ButtonWriteBinary_Click()
DIM filePath AS String
' Use a temporary file
filePath = Temp()
' Write binary file
BinaryWrite(filePath)
' Display binary file
BinaryRead(filePath)
' Remove temporary file
KILL filePath
CATCH
Message.Error(Error.Text)
END
PRIVATE SUB BinaryWrite(FilePath AS String)
DIM binaryFile AS File
DIM i AS Integer = 10
DIM b AS Byte = 4
DIM s AS Short = 23
DIM s1 AS String = "This is string 1"
DIM s2 AS String = "Another string"
' Open as create so we get a new file
binaryFile = OPEN FilePath FOR CREATE
WRITE #binaryFile, i
WRITE #binaryFile, b
WRITE #binaryFile, s
WRITE #binaryFile, s1
WRITE #binaryFile, s2
CLOSE #binaryFile
END
PRIVATE SUB BinaryRead(FilePath AS String)
DIM binaryFile AS File
DIM i AS Integer
DIM b AS Byte
DIM s AS Short
DIM s1 AS String
DIM s2 AS String
' Read binary file
binaryFile = OPEN FilePath FOR READ
READ #binaryFile, i
READ #binaryFile, b
READ #binaryFile, s
READ #binaryFile, s1
READ #binaryFile, s2
CLOSE #binaryFile
' Display results
PRINT i
PRINT b
PRINT s
PRINT s1
PRINT s2
END