Quirrel4.41.0

iostream.stream.writestring

Method of stream, from module "iostream"
instance.writestring(str: string): int

Writes the string and returns the number of characters written

Parameters

strstringthe string to write

Return value

The number of bytes written: str's byte length, the same number str.len() reports. A string holding multi-byte UTF-8 text counts its encoded bytes, not its number of code points.

Errors

Throws io error if the underlying stream cannot store the full amount, for example a file opened read-only; a blob's own storage never refuses a write, so this case cannot happen on a blob.

Notes

The same method works on a file. Writing past the current len grows a blob to fit, the same as writen; writing past the end of a writable file extends it the same way.

Example

examples/iostream/stream/writestring.nut
from "iostream" import blob

let b = blob(0)
println("b.writestring(\"hi\") =", b.writestring("hi")) // the number of bytes written
println("b.len() after writestring(\"hi\") =", b.len())

let e = "\xC3\xA9"       // one letter, 2 UTF-8 bytes
println("b.writestring(e) =", b.writestring(e)) // counts bytes, not code points
println("b.len() after writestring(e) =", b.len())
Output:
b.writestring("hi") = 2
b.len() after writestring("hi") = 2
b.writestring(e) = 2
b.len() after writestring(e) = 4

See also

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