io.file.writeobject
Serializes the object to the stream
Parameters
obj | any | the value to serialize |
classes | table|null | classes 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
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() = helloSee also
writeobject | Serializes the object to the stream |
readobject | Deserializes an object from the stream |
constructor | Two 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 |
| file | class index |