| 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 gets n 7.5 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 |
gets \- Read a line from a channel |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBgets \fIchannelId\fR ?\fIvarName\fR? |
| 16 |
.BE |
| 17 |
|
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
This command reads the next line from \fIchannelId\fR, returns everything |
| 21 |
in the line up to (but not including) the end-of-line character(s), and |
| 22 |
discards the end-of-line character(s). |
| 23 |
.PP |
| 24 |
\fIChannelId\fR must be an identifier for an open channel such as the |
| 25 |
Tcl standard input channel (\fBstdin\fR), the return value from an |
| 26 |
invocation of \fBopen\fR or \fBsocket\fR, or the result of a channel |
| 27 |
creation command provided by a Tcl extension. The channel must have |
| 28 |
been opened for input. |
| 29 |
.PP |
| 30 |
If \fIvarName\fR is omitted the line is returned as the result of the |
| 31 |
command. |
| 32 |
If \fIvarName\fR is specified then the line is placed in the variable by |
| 33 |
that name and the return value is a count of the number of characters |
| 34 |
returned. |
| 35 |
.PP |
| 36 |
If end of file occurs while scanning for an end of |
| 37 |
line, the command returns whatever input is available up to the end of file. |
| 38 |
If \fIchannelId\fR is in non-blocking mode and there is not a full |
| 39 |
line of input available, the command returns an empty string and |
| 40 |
does not consume any input. |
| 41 |
If \fIvarName\fR is specified and an empty string is returned in |
| 42 |
\fIvarName\fR because of end-of-file or because of insufficient |
| 43 |
data in non-blocking mode, then the return count is -1. |
| 44 |
Note that if \fIvarName\fR is not specified then the end-of-file |
| 45 |
and no-full-line-available cases can |
| 46 |
produce the same results as if there were an input line consisting |
| 47 |
only of the end-of-line character(s). |
| 48 |
The \fBeof\fR and \fBfblocked\fR commands can be used to distinguish |
| 49 |
these three cases. |
| 50 |
.SH "EXAMPLE" |
| 51 |
This example reads a file one line at a time and prints it out with |
| 52 |
the current line number attached to the start of each line. |
| 53 |
.PP |
| 54 |
.CS |
| 55 |
set chan [open "some.file.txt"] |
| 56 |
set lineNumber 0 |
| 57 |
while {[\fBgets\fR $chan line] >= 0} { |
| 58 |
puts "[incr lineNumber]: $line" |
| 59 |
} |
| 60 |
close $chan |
| 61 |
.CE |
| 62 |
|
| 63 |
.SH "SEE ALSO" |
| 64 |
file(n), eof(n), fblocked(n), Tcl_StandardChannels(3) |
| 65 |
|
| 66 |
.SH KEYWORDS |
| 67 |
blocking, channel, end of file, end of line, line, non-blocking, read |
| 68 |
'\" Local Variables: |
| 69 |
'\" mode: nroff |
| 70 |
'\" fill-column: 78 |
| 71 |
'\" End: |