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