writeFileTcl 9.1 onlychannels
Writes a string as the whole content of a file.
writeFile NAME ?MODE? DATA
Description
Opens the file NAME for writing, writes DATA, and closes it. A file that exists loses its old content, and a file that does not exist is made. No newline goes after DATA.
MODE is text (the default) or binary, or a prefix of one of them. text writes with the defaults of open: the encoding UTF-8 and the line end of the platform, so each \n is \r\n on Windows. binary writes each character as one byte, the low 8 bits of its code.
The command is in Tcl 9.1 only. A new interpreter has no writeFile: the host turns on the capability channels.
Arguments
| Argument | Type | Default | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| NAME | path | required | The path of the file. | ||||||
| MODE | keyword | text | How to write the data: as text, or as bytes. Values:
| ||||||
| DATA | string | required | The new content of the file. |
Returns
An empty string.
Examples
% writeFile w.txt hello; readFile w.txt
hello
% writeFile w.txt binary "a\nb\n"; file size w.txt
4
# One character of two bytes in UTF-8.
% writeFile w.txt "é"; file size w.txt
2
% writeFile w.txt binary "é"; file size w.txt
1
# The second write replaces the content.
% writeFile w.txt long; writeFile w.txt x; readFile w.txt
x
Errors
% writeFile w.txt bogus x
bad mode "bogus": must be binary or text
-errorcode TCL LOOKUP MODE bogus
# The path leaves the root folder.
% writeFile ../x.txt hi
couldn't open "../x.txt": permission denied
-errorcode POSIX EACCES {permission denied}
% writeFile w.txt
wrong # args: should be "writeFile filename ?mode? data"
-errorcode TCL WRONGARGS
Differences from Tcl
- In tclsh 9.1,
writeFileis a proc. In ztcl it is a command, andinfo bodygives an error.
Facts
- Capability
channels. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 only.
- Since
- ztcl 1.0.0-beta.3