| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH read n 8.1 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
read \- Read from a channel |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBread \fR?\fB\-nonewline\fR? \fIchannelId\fR |
| 16 |
.sp |
| 17 |
\fBread \fIchannelId numChars\fR |
| 18 |
.BE |
| 19 |
.SH DESCRIPTION |
| 20 |
.PP |
| 21 |
In the first form, the \fBread\fR command reads all of the data from |
| 22 |
\fIchannelId\fR up to the end of the file. If the \fB\-nonewline\fR |
| 23 |
switch is specified then the last character of the file is discarded |
| 24 |
if it is a newline. In the second form, the extra argument specifies |
| 25 |
how many characters to read. Exactly that many characters will be |
| 26 |
read and returned, unless there are fewer than \fInumChars\fR left in |
| 27 |
the file; in this case all the remaining characters are returned. If |
| 28 |
the channel is configured to use a multi-byte encoding, then the |
| 29 |
number of characters read may not be the same as the number of bytes |
| 30 |
read. |
| 31 |
.PP |
| 32 |
\fIChannelId\fR must be an identifier for an open channel such as the |
| 33 |
Tcl standard input channel (\fBstdin\fR), the return value from an |
| 34 |
invocation of \fBopen\fR or \fBsocket\fR, or the result of a channel |
| 35 |
creation command provided by a Tcl extension. The channel must have |
| 36 |
been opened for input. |
| 37 |
.PP |
| 38 |
If \fIchannelId\fR is in nonblocking mode, the command may not read as |
| 39 |
many characters as requested: once all available input has been read, |
| 40 |
the command will return the data that is available rather than |
| 41 |
blocking for more input. If the channel is configured to use a |
| 42 |
multi-byte encoding, then there may actually be some bytes remaining |
| 43 |
in the internal buffers that do not form a complete character. These |
| 44 |
bytes will not be returned until a complete character is available or |
| 45 |
end-of-file is reached. The \fB\-nonewline\fR switch is ignored if |
| 46 |
the command returns before reaching the end of the file. |
| 47 |
.PP |
| 48 |
\fBRead\fR translates end-of-line sequences in the input into |
| 49 |
newline characters according to the \fB\-translation\fR option |
| 50 |
for the channel. |
| 51 |
See the \fBfconfigure\fR manual entry for a discussion on ways in |
| 52 |
which \fBfconfigure\fR will alter input. |
| 53 |
.SH "USE WITH SERIAL PORTS" |
| 54 |
'\" Note: this advice actually applies to many versions of Tcl |
| 55 |
.PP |
| 56 |
For most applications a channel connected to a serial port should be |
| 57 |
configured to be nonblocking: \fBfconfigure\fI channelId \fB\-blocking |
| 58 |
\fI0\fR. Then \fBread\fR behaves much like described above. Care |
| 59 |
must be taken when using \fBread\fR on blocking serial ports: |
| 60 |
.TP |
| 61 |
\fBread \fIchannelId numChars\fR |
| 62 |
. |
| 63 |
In this form \fBread\fR blocks until \fInumChars\fR have been received |
| 64 |
from the serial port. |
| 65 |
.TP |
| 66 |
\fBread \fIchannelId\fR |
| 67 |
. |
| 68 |
In this form \fBread\fR blocks until the reception of the end-of-file |
| 69 |
character, see \fBfconfigure\fR \fB\-eofchar\fR. If there no end-of-file |
| 70 |
character has been configured for the channel, then \fBread\fR will |
| 71 |
block forever. |
| 72 |
.SH "EXAMPLE" |
| 73 |
.PP |
| 74 |
This example code reads a file all at once, and splits it into a list, |
| 75 |
with each line in the file corresponding to an element in the list: |
| 76 |
.PP |
| 77 |
.CS |
| 78 |
set fl [open /proc/meminfo] |
| 79 |
set data [\fBread\fR $fl] |
| 80 |
close $fl |
| 81 |
set lines [split $data \en] |
| 82 |
.CE |
| 83 |
.SH "SEE ALSO" |
| 84 |
file(n), eof(n), fblocked(n), fconfigure(n), Tcl_StandardChannels(3) |
| 85 |
.SH KEYWORDS |
| 86 |
blocking, channel, end of line, end of file, nonblocking, read, translation, encoding |
| 87 |
'\"Local Variables: |
| 88 |
'\"mode: nroff |
| 89 |
'\"End: |