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
| Argument | Type | Default | Description |
|---|---|---|---|
| CHAN | channel | required | The channel to read. |
| COUNT | integer, 0 or more | none | The most characters to read. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-nonewline | flag | none | Drops 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
helloA 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