Quirrel4.41.0

io.file.readblob

Method of file, from module "io"
instance.readblob(size: int): instance

Reads up to size bytes and returns them as a blob

examples/io/file/readblob-basic.nut
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() = ammo

Parameters

sizeintthe 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

examples/io/file/readblob.nut
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 read

See also

readblobReads up to size bytes and returns them as a blob
writeblobWrites the given blob and returns the number of bytes written
readnReads a value of the given numeric format and returns it
fileclass index