Quirrel4.41.0

io.file.writestring

Method of file, from module "io"
instance.writestring(str: string): int

Writes the string and returns the number of characters written

Parameters

strstringthe string to write

Return value

See iostream.stream.writestring for the generic contract.

Errors

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

Notes

The count returned is the raw byte length of str; it is exact only in a b mode. In a text mode (see constructor), the C library can insert an extra byte per line ending, so more bytes can land on disk than the returned count says were written.

Example

examples/io/file/writestring.nut
from "io" import file
from "system" import getenv, remove

let path = $"{getenv("TEMP") ?? getenv("TMP") ?? "/tmp"}/qrl_io_file_writestring.tmp"
let f = file(path, "wb")             // binary mode: no line-ending translation
println("f.writestring(\"ab\\ncd\") =", f.writestring("ab\ncd"))       // returns the character count, 5
f.close()

let r = file(path, "rb")
println("r.len() =", r.len())                       // same 5 bytes landed on disk
r.close()
remove(path)
Output:
f.writestring("ab\ncd") = 5
r.len() = 5

See also

writestringWrites the string and returns the number of characters written
writeblobWrites the given blob and returns the number of bytes written
constructorTwo forms: (path: string, mode: string) opens the file via fopen with the given mode; (handle: userpointer, own) wraps an existing FILE* and takes ownership (closes on destruction) when own is non-null, or shares it without closing when own is null
fileclass index