Quirrel4.41.0

io.file.readobject

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

Deserializes an object from the stream

Parameters

classestable|nullclasses the serialized data is allowed to instantiate optional

Return value

See iostream.stream.readobject for the generic contract.

Errors

Throws the stream is invalid once the file has been closed. See iostream.stream.readobject for the deserialization errors, such as reading data that writeobject did not produce.

Notes

Pairing writeobject and readobject on a file persists a value across runs, not just within one script: write it, close that handle, and a later run opens the same path to read it back. Reading it back with the same still-open handle also works, provided the cursor is moved back first (see seek) - the same rule as reading anything else just written.

Example

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

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_readobject.tmp"
let w = file(path, "wb")
w.writeobject([1, 2, 3])
w.close()

let r = file(path, "rb")
let v = r.readobject()      // the array survives the round trip through disk
println(v[0] + v[1] + v[2])
r.close()

remove(path)
Output:
6

See also

readobjectDeserializes an object from the stream
writeobjectSerializes the object to 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