Quirrel4.41.0

io.file.writeblob

Method of file, from module "io"
instance.writeblob(blob: instance): int

Writes the given blob and returns the number of bytes written

examples/io/file/writeblob-basic.nut
from "io" import file
from "iostream" import blob
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writeblob_basic.tmp"
let cargo = blob(0)
cargo.writestring("ammo")

let f = file(path, "wb")
println("f.writeblob(cargo) =", f.writeblob(cargo))

f.close()
remove(path)
Output:
f.writeblob(cargo) = 4

Parameters

blobinstancethe source blob; all of it is written

Return value

See iostream.stream.writeblob for the generic contract.

Errors

Throws the stream is invalid once the file has been closed.

Notes

Writing past the current end of the file grows it, the same as writen; it is never an error to write more than currently fits, since a real file has no fixed capacity to run into.

Example

examples/io/file/writeblob.nut
from "io" import file
from "iostream" import blob
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writeblob.tmp"
let b = blob(3)
b.writen(0x41, 'b')
b.writen(0x42, 'b')
b.writen(0x43, 'b')

let f = file(path, "wb")
println("f.writeblob(b) =", f.writeblob(b))  // all of it goes, and the count written comes back
f.close()

let r = file(path, "rb")
println("r.len() =", r.len())
r.close()
remove(path)
Output:
f.writeblob(b) = 3
r.len() = 3

See also

writeblobWrites the given blob and returns the number of bytes written
readblobReads up to size bytes and returns them as a blob
writestringWrites the string and returns the number of characters written
fileclass index