1read(n) Tcl Built-In Commands read(n)
2
3
4
5______________________________________________________________________________
6
8 read - Read from a channel
9
11 read ?-nonewline? channelId
12
13 read channelId numChars
14_________________________________________________________________
15
16
18 In the first form, the read command reads all of the data from chan‐
19 nelId up to the end of the file. If the -nonewline switch is specified
20 then the last character of the file is discarded if it is a newline.
21 In the second form, the extra argument specifies how many characters to
22 read. Exactly that many characters will be read and returned, unless
23 there are fewer than numChars left in the file; in this case all the
24 remaining characters are returned. If the channel is configured to use
25 a multi-byte encoding, then the number of characters read may not be
26 the same as the number of bytes read.
27
28 ChannelId must be an identifier for an open channel such as the Tcl
29 standard input channel (stdin), the return value from an invocation of
30 open or socket, or the result of a channel creation command provided by
31 a Tcl extension. The channel must have been opened for input.
32
33 If channelId is in nonblocking mode, the command may not read as many
34 characters as requested: once all available input has been read, the
35 command will return the data that is available rather than blocking for
36 more input. If the channel is configured to use a multi-byte encoding,
37 then there may actually be some bytes remaining in the internal buffers
38 that do not form a complete character. These bytes will not be
39 returned until a complete character is available or end-of-file is
40 reached. The -nonewline switch is ignored if the command returns
41 before reaching the end of the file.
42
43 Read translates end-of-line sequences in the input into newline charac‐
44 ters according to the -translation option for the channel. See the
45 fconfigure manual entry for a discussion on ways in which fconfigure
46 will alter input.
47
48
50 For most applications a channel connected to a serial port should be
51 configured to be nonblocking: fconfigure channelId -blocking 0. Then
52 read behaves much like described above. Care must be taken when using
53 read on blocking serial ports:
54
55 read channelId numChars
56 In this form read blocks until numChars have been received from
57 the serial port.
58
59 read channelId
60 In this form read blocks until the reception of the end-of-file
61 character, see fconfigure -eofchar. If there no end-of-file
62 character has been configured for the channel, then read will
63 block forever.
64
66 This example code reads a file all at once, and splits it into a list,
67 with each line in the file corresponding to an element in the list:
68 set fl [open /proc/meminfo]
69 set data [read $fl]
70 close $fl
71 set lines [split $data \n]
72
73
75 file(n), eof(n), fblocked(n), fconfigure(n), Tcl_StandardChannels(3)
76
77
79 blocking, channel, end of line, end of file, nonblocking, read, trans‐
80 lation, encoding
81
82
83
84Tcl 8.1 read(n)