Quirrel4.41.0

io.file.readn

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

Reads a value of the given numeric format and returns it

Parameters

formatinta character selecting the type and size to read

Return value

See iostream.stream.readn for the format codes, their sizes and the byte order used.

Errors

Throws io error when fewer bytes than the format needs are left before len. Throws the stream is invalid once the file has been closed.

Notes

The bytes come straight from the OS file, in whatever mode the file was opened with; 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/readn.nut
from "io" import file
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_readn.tmp"
let f = file(path, "w+b")
f.writen(0x01020304, 'i')
f.seek(0)
println("f.readn('i') =", f.readn('i'))   // native byte order, same value back

f.seek(3)                // only 1 byte left before len, 'i' needs 4
try { f.readn('i'); }
catch (e) { println("f.readn('i') at offset 3 throws:", e); }

f.close()
remove(path)
Output:
f.readn('i') = 16909060
f.readn('i') at offset 3 throws: io error

See also

readnReads a value of the given numeric format and returns it
writenWrites a numeric value in the given format
seekSeeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end)
fileclass index