io.file.readblob
Reads up to size bytes and returns them as a blob
from "io" import file
from "system" import getenv, remove
let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_readblob_basic.tmp"
let f = file(path, "w+")
f.writestring("ammo")
f.seek(0)
println("f.readblob(4).as_string() =", f.readblob(4).as_string())
f.close()
remove(path)Output:
f.readblob(4).as_string() = ammoParameters
size | int | the largest number of bytes to read |
Return value
See iostream.stream.readblob for the generic contract.
Errors
Throws no data left to read when the cursor is already at len and nothing can be read. Throws the stream is invalid once the file has been closed.
Notes
size is capped against len, not against what is left after the cursor, so asking for more than remains is not an error by itself: fewer bytes than requested come back in the result blob. Only asking with nothing left throws.
Example
from "io" import file
from "system" import getenv, remove
let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_readblob.tmp"
let f = file(path, "w+")
f.writestring("abcde")
f.seek(3)
println("f.readblob(5).len() =", f.readblob(5).len()) // only 2 bytes remain, so fewer come back than asked
try { f.readblob(1); }
catch (e) { println("f.readblob(1) throws:", e); } // now truly at the end: nothing at all to read
f.close()
remove(path)Output:
f.readblob(5).len() = 2
f.readblob(1) throws: no data left to readSee also
readblob | Reads up to size bytes and returns them as a blob |
writeblob | Writes the given blob and returns the number of bytes written |
readn | Reads a value of the given numeric format and returns it |
| file | class index |