Quirrel4.41.0

io.file.writeobject

Method of file, from module "io"
instance.writeobject(obj, [classes: table|null])

Serializes the object to the stream

Parameters

objanythe value to serialize
classestable|nullclasses the serialized data is allowed to instantiate optional

Errors

Throws the stream is invalid once the file has been closed. See iostream.stream.writeobject for the serialization errors.

Notes

See iostream.stream.writeobject for the generic contract. close the file (or flush it) before another process or another file instance tries to read the bytes back with readobject.

Example

examples/io/file/writeobject.nut
from "io" import file
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writeobject.tmp"
let w = file(path, "wb")
w.writeobject("hello")
println("w.tell() > 0 =", w.tell() > 0)   // the marker and payload took some real bytes
w.close()

let r = file(path, "rb")
println("r.readobject() =", r.readobject())  // read it back with io.file.readobject
r.close()

remove(path)
Output:
w.tell() > 0 = true
r.readobject() = hello

See also

writeobjectSerializes the object to the stream
readobjectDeserializes an object from the stream
constructorTwo forms: (path: string, mode: string) opens the file via fopen with the given mode; (handle: userpointer, own) wraps an existing FILE* and takes ownership (closes on destruction) when own is non-null, or shares it without closing when own is null
fileclass index