WRITE
 |
WARNING! The syntax has changed in Gambas 3.
|
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, the string length is directly written to the
stream just before the string data.
 |
This instruction uses the byte order of the stream to write the data.
|
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.
|
Beware with the datatype of the Expression argument
 |
The datatype of Expression is used to know what to write exactly to the stream. Beware that it is not necessarily what you think.
For example, if you have an anonymous reference on an object (i.e. if you use the Object datatype), then the datatype of any of its properties will be Variant, not its real datatype. And so WRITE will write a Variant, not what you expected.
If you use an object reference whose datatype is declared at compilation time, you won't have this problem.
If you are not sure, the safest way is to use an intermediate local variable with a well-known datatype, or an explicit conversion.
DIM hStream AS Stream ' The stream
DIM hObject AS Object ' An object with a float property named "Value"
DIM fValue AS Float ' We want to write a float property
' The bad way
WRITE #hStream, hObject.Value
' The good way
fValue = hObject.Value
WRITE #hStream, fValue
' Another good way
WRITE #hStream, CFloat(hObject.Value)
|
Example
... ' 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