close.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:3k
- '"
- '" Copyright (c) 1993 The Regents of the University of California.
- '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: close.n,v 1.4.8.1 2004/10/27 09:35:38 dkf Exp $
- '"
- .so man.macros
- .TH close n 7.5 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- close - Close an open channel.
- .SH SYNOPSIS
- fBclose fIchannelIdfR
- .BE
- .SH DESCRIPTION
- .PP
- Closes the channel given by fIchannelIdfR.
- .PP
- .VS
- fIChannelIdfR must be an identifier for an open channel such as a
- Tcl standard channel (fBstdinfR, fBstdoutfR, or fBstderrfR),
- the return value from an invocation of fBopenfR or fBsocketfR, or
- the result of a channel creation command provided by a Tcl extension.
- .VE
- .PP
- All buffered output is flushed to the channel's output device,
- any buffered input is discarded, the underlying file or device is closed,
- and fIchannelIdfR becomes unavailable for use.
- .VS "" br
- .PP
- If the channel is blocking, the command does not return until all output
- is flushed.
- If the channel is nonblocking and there is unflushed output, the
- channel remains open and the command
- returns immediately; output will be flushed in the background and the
- channel will be closed when all the flushing is complete.
- .VE
- .PP
- If fIchannelIdfR is a blocking channel for a command pipeline then
- fBclosefR waits for the child processes to complete.
- .VS "" br
- .PP
- If the channel is shared between interpreters, then fBclosefR
- makes fIchannelIdfR unavailable in the invoking interpreter but has no
- other effect until all of the sharing interpreters have closed the
- channel.
- When the last interpreter in which the channel is registered invokes
- fBclosefR, the cleanup actions described above occur. See the
- fBinterpfR command for a description of channel sharing.
- .PP
- Channels are automatically closed when an interpreter is destroyed and
- when the process exits. Channels are switched to blocking mode, to ensure
- that all output is correctly flushed before the process exits.
- .VE
- .PP
- The command returns an empty string, and may generate an error if
- an error occurs while flushing output. If a command in a command
- pipeline created with fBopenfR returns an error, fBclosefR
- generates an error (similar to the fBexecfR command.)
- .SH EXAMPLE
- This illustrates how you can use Tcl to ensure that files get closed
- even when errors happen by combining fBcatchfR, fBclosefR and
- fBreturnfR:
- .CS
- proc withOpenFile {filename channelVar script} {
- upvar 1 $channelVar chan
- set chan [open $filename]
- catch {
- uplevel 1 $script
- } result options
- fBclosefR $chan
- return -options $options $result
- }
- .CE
- .SH "SEE ALSO"
- file(n), open(n), socket(n), eof(n), Tcl_StandardChannels(3)
- .SH KEYWORDS
- blocking, channel, close, nonblocking