chan gets

Reads one line from a channel.

chan gets CHAN ?VAR?

Description

Reads the next line of CHAN, with no newline at its end. With VAR it puts the line in VAR and gives its count of characters, or -1 at the end of the channel. With no VAR it gives the line, or an empty string at the end; use chan eof to tell an empty line from the end.

Arguments

ArgumentTypeDefaultDescription
CHANchannelrequiredThe channel to read.
VARvariable namenoneA variable for the line.

Returns

The line, or with VAR the count of its characters or -1.

Examples

% set f [open a.txt w]; chan puts $f hello; chan close $f
> set f [open a.txt]
> set l [chan gets $f]
> chan close $f
> set l
hello

% set f [open a.txt]
> chan gets $f line
> set n [chan gets $f line2]
> chan close $f
> list $line $n
hello -1

Errors

% chan gets nochan
can not find channel named "nochan"
-errorcode TCL LOOKUP CHANNEL nochan

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