io.file.readn
Reads a value of the given numeric format and returns it
Parameters
format | int | a 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
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 errorSee also
readn | Reads a value of the given numeric format and returns it |
writen | Writes a numeric value in the given format |
seek | Seeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end) |
| file | class index |