chan read

Reads the rest of a channel, or a count of characters.

chan read ?-nonewline? CHAN
chan read CHAN COUNT

Description

Reads CHAN to its end and gives the text. With COUNT it reads at most COUNT characters. At the end of the channel the result is an empty string.

Arguments

ArgumentTypeDefaultDescription
CHANchannelrequiredThe channel to read.
COUNTinteger, 0 or morenoneThe most characters to read.

Options

OptionTypeDefaultSummary
-nonewlineflagnoneDrops the newline at the end of the text.

-nonewline

Drops the newline at the end of the text.

Reads to the end and drops one newline at the end of the text, when it has one.

% set f [open a.txt]
> set d [chan read -nonewline $f]
> chan close $f
> set d
hello

A flag has no value to check.

Returns

The text that was read.

Examples

% set f [open a.txt w]; chan puts $f hello; chan close $f
> set f [open a.txt]
> set d [chan read $f 3]
> chan close $f
> set d
hel

% set f [open a.txt]
> set d [chan read $f]
> chan close $f
> set d
hello

Errors

% set f [open a.txt]
> catch {chan read $f x} m
> chan close $f
> error $m
expected non-negative integer but got "x"
-errorcode NONE

Facts

Capability
channels. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also