io.file.writen
Writes a numeric value in the given format
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') = 200Parameters
value | number | the value to write |
format | int | a 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
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 = 4See also
writen | Writes a numeric value in the given format |
readn | Reads a value of the given numeric format and returns it |
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 |