Home / lang / write 
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.

This instruction uses the byte order of the stream to read 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.

This example shows how we can write a binary file. It then READ's the file created so we can display the file content.

Example
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


See also
Stream & Input/Output functions  Stream.ByteOrder  Binary Data Representation  External Function Management