Quirrel4.41.0

io.file.writen

Method of file, from module "io"
instance.writen(value: number, format: int)

Writes a numeric value in the given format

examples/io/file/writen-basic.nut
from "io" import file
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writen_basic.tmp"
let f = file(path, "w+b")
f.writen(200, 'i')

f.seek(0)
println("f.readn('i') =", f.readn('i'))

f.close()
remove(path)
Output:
f.readn('i') = 200

Parameters

valuenumberthe value to write
formatinta character selecting the type and size to write

Errors

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

Notes

See iostream.stream.writen for the format codes, their sizes and the byte order used. Open with a b mode (see constructor) so no line-ending translation can land an unexpected byte in the middle of a value.

Example

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

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writen.tmp"
let f = file(path, "w+b")
f.writen(0x01020304, 'i')
println("f.len() after writen =", f.len())      // 4 bytes for a 32-bit 'i', same sizes as iostream.stream.writen

f.close()
remove(path)
Output:
f.len() after writen = 4

See also

writenWrites a numeric value in the given format
readnReads a value of the given numeric format and returns it
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