Quirrel4.41.0

io.file.flush

Method of file, from module "io"
instance.flush(): int|null

Flushes the stream and returns non-null on success

Return value

See iostream.stream.flush for the generic contract. Unlike a blob, a file has a real buffer to flush, so this does real work and, in principle, can fail.

Errors

Throws the stream is invalid once the file has been closed.

Notes

close flushes too, so an explicit flush before closing is only useful to make data visible to another reader of the same file while this handle stays open.

Example

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

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_flush.tmp"
let f = file(path, "w")
f.writestring("data")
println("f.flush() =", f.flush())   // non-null: the OS buffer for this handle was flushed

f.close()
try { f.flush(); }
catch (e) { println("f.flush() after close throws:", e); }   // flushing a closed file is still an invalid stream

remove(path)
Output:
f.flush() = 1
f.flush() after close throws: the stream is invalid

See also

flushFlushes the stream and returns non-null on success
closeCloses the file if it is still open
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