beetlebug▾
/

readFileTcl 9.1 onlychannels

Gives the whole content of a file.

readFile NAME ?MODE?

Description

Opens the file NAME, reads it to its end, closes it, and gives the content. The file closes also after an error of the read.

MODE is text (the default) or binary, or a prefix of one of them. text reads with the defaults of open: the encoding UTF-8 and the translation auto, so each line end is \n. binary reads the bytes, each one as a character from U+0000 to U+00FF.

The command is in Tcl 9.1 only. A new interpreter has no readFile: the host turns on the capability channels.

Arguments

ArgumentTypeDefaultDescription
NAMEpathrequiredThe path of the file.
MODEkeywordtextHow to read the file: as text, or as bytes.

Values: text, binary.

ValueDescription
textDecodes UTF-8, and makes each line end \n.
binaryGives each byte as one character.

Returns

The content of the file.

Examples

% writeFile r.txt "a\nb\n"; string length [readFile r.txt]
4

% readFile r.txt
a
b


# One character of two bytes in UTF-8.
% writeFile u.txt "é"; list [string length [readFile u.txt]] [string length [readFile u.txt binary]]
1 2

% writeFile e.txt ""; readFile e.txt

# A prefix of a mode is the mode.
% string length [readFile u.txt b]
2

Errors

% readFile r.txt bogus
bad mode "bogus": must be binary or text
-errorcode TCL LOOKUP MODE bogus

% readFile nope.txt
couldn't open "nope.txt": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}

% info body readFile
"readFile" isn't a procedure
-errorcode NONE

% readFile
wrong # args: should be "readFile filename ?mode?"
-errorcode TCL WRONGARGS

Differences from Tcl

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

See also