1close(n) Tcl Built-In Commands close(n)
2
3
4
5______________________________________________________________________________
6
8 close - Close an open channel.
9
11 close channelId
12_________________________________________________________________
13
14
16 Closes the channel given by channelId.
17
18 ChannelId must be an identifier for an open channel such as a Tcl stan‐ │
19 dard channel (stdin, stdout, or stderr), the return value from an invo‐ │
20 cation of open or socket, or the result of a channel creation command │
21 provided by a Tcl extension.
22
23 All buffered output is flushed to the channel's output device, any
24 buffered input is discarded, the underlying file or device is closed,
25 and channelId becomes unavailable for use.
26
27 If the channel is blocking, the command does not return until all out‐ │
28 put is flushed. If the channel is nonblocking and there is unflushed │
29 output, the channel remains open and the command returns immediately; │
30 output will be flushed in the background and the channel will be closed │
31 when all the flushing is complete.
32
33 If channelId is a blocking channel for a command pipeline then close
34 waits for the child processes to complete.
35
36 If the channel is shared between interpreters, then close makes chan‐ │
37 nelId unavailable in the invoking interpreter but has no other effect │
38 until all of the sharing interpreters have closed the channel. When │
39 the last interpreter in which the channel is registered invokes close, │
40 the cleanup actions described above occur. See the interp command for a │
41 description of channel sharing. │
42
43 Channels are automatically closed when an interpreter is destroyed and │
44 when the process exits. Channels are switched to blocking mode, to │
45 ensure that all output is correctly flushed before the process exits.
46
47 The command returns an empty string, and may generate an error if an
48 error occurs while flushing output. If a command in a command pipeline
49 created with open returns an error, close generates an error (similar
50 to the exec command.)
51
53 This illustrates how you can use Tcl to ensure that files get closed
54 even when errors happen by combining catch, close and return:
55 proc withOpenFile {filename channelVar script} {
56 upvar 1 $channelVar chan
57 set chan [open $filename]
58 catch {
59 uplevel 1 $script
60 } result options
61 close $chan
62 return -options $options $result
63 }
64
65
67 file(n), open(n), socket(n), eof(n), Tcl_StandardChannels(3)
68
69
71 blocking, channel, close, nonblocking
72
73
74
75Tcl 7.5 close(n)