io.file.writestring
Writes the string and returns the number of characters written
Parameters
str | string | the 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
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() = 5See also
writestring | Writes the string and returns the number of characters written |
writeblob | Writes the given blob and returns the number of bytes written |
constructor | Two 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 |
| file | class index |