📄 28.html
字号:
<HTML><TITLE>Basic Syntax and I/O: Opening Files</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Opening Files</H1></A>
<P> To finish the file copying example, we need to open the input and output
files. Assuming the input and output files are called <TT>input.txt</TT> and
<TT>output.txt</TT> respectively, the relevant procedure invocations are
<PRE>
open input.txt r
open output.txt w
</PRE>
<P> The <TT><NAME=#Copen>open</A></TT> procedure sets things up so a file can be read or
written and returns a string which can be used to identify that file. This
open-file identifier should be saved in a variable for use with <TT>gets</TT> and
<TT>puts.</TT>
<P> Use the <TT>open</TT> procedure with two arguments: the first argument identifies
the file and the second argument identifies the way in which the file will be
used, for example, for reading or writing.
<P>
<P> <STRONG>Remark</STRONG> <DL><DD>
<P> There is a more expressive way of specifying file usage by making the second
argument a list whose first element is <TT>RDONLY</TT>, <TT>WRONLY</TT>, or <TT>RDWR</TT>
for "read only," "write only," and "read-write." Other elements to
this list are optional and include: <TT>CREAT</TT> to create the file if it
does not exist, <TT>EXCL</TT> for use with <TT>CREAT</TT> if you want an error if the file
already exists, <TT>APPEND</TT> to position the file pointer at the end of the
file, and <TT>TRUNC</TT> to truncate a file to zero length if it exists.
<P> As examples of this more expressive notation, the following two pairs
of statements are equivalent.
<PRE>
open input.txt r
open input.txt {RDONLY}
open output.txt w
open output.txt {WRONLY CREAT TRUNC}
</PRE>
</DL>
<P> Here is the way the input and output files will be opened in the file copying
example.
<PRE>
set InFile [open input.txt r]
set OutFile [open output.txt w]
</PRE>
Notice the way command substitution permits you to obtain the proper file
identifiers to be assigned to the variables.
<P> <P><A NAME="2.8a">
<STRONG>Exercise 2.8a</STRONG> </A><DL><DD>
Suppose that variables named <TT>InputFileName</TT>
and <TT>OutputFileName</TT> contain names of the input and output files,
respectively. Rewrite the two file opening statements to use these
variables rather than hard-coded names.
<P>
<A HREF="2.11.html#Sol2.8a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.8a">Solution</A></DL>
<P> When the Tcl interpreter starts executing, three open files are already
opened for you. These are designated <TT><NAME=#G2.8stdin>stdin</A>,</TT>
<TT><NAME=#G2.8stdout>stdout</A>,</TT> and <TT><NAME=#G2.8stderr>stderr</A>.</TT> Normally, the <TT>stdin</TT>
takes input from the window that is running Tcl, whereas the <TT>stdout</TT> and
<TT>stderr</TT> write output there. Use <TT>stdout</TT> for normal output and
<TT>stderr</TT> for error messages.
<P> <STRONG>Remark</STRONG> <DL><DD> See above in
<A HREF="1.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/1.3.html">Direct Execution</A> for a situation in which these
standard files are not available. Also, be aware that they can be redirected
by your operating system in the same way that other files can be redirected.
</DL>
<P> Tcl also has a <TT><NAME=#Cclose>close</A></TT> procedure. For example, these commands
will close the files opened above
<PRE>
close $InFile
close $OutFile
</PRE>
When Tcl finishes executing, it closes any files you forgot to close. It
also closes any file when you try to re-open that file.
<P> <STRONG>Remark</STRONG> <DL><DD>
<P> More about input and output statements can be found above in
<A HREF="2.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.3.html">Copying a File Line by Line</A>
(<TT>gets</TT> and <TT>puts</TT>) and below in
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">The <TT>file</TT> Action Family</A> (<TT>seek</TT>),
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">Starting Processes and Pipes</A>
(<TT>open</TT>), and
<A HREF="NotHere.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/NotHere.html">More about I/O</A> ( <TT>fconfigure,</TT> <TT>flush</TT>, <TT>read</TT>,
<TT>fblocked</TT>, and <TT>socket</TT>). </DL>
<!-- Linkbar -->
<P><CENTER><FONT SIZE=2><NOBR>
<STRONG>From</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/sbf/tcl/book/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/sbf/tcl/book/home.html'" tppabs="http://www.mapfree.com/sbf/tcl/book/home.html">Tcl/Tk For Programmers</A><WBR>
<STRONG>Previous</STRONG>
<A HREF="2.7.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.7.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.9.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.9.html">section</A><WBR>
<STRONG>All</STRONG>
<A HREF="2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.html">sections</A><WBR>
<STRONG>Author</STRONG>
<A HREF="javascript:if(confirm('http://www.mapfree.com/mp/jaz/home.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mapfree.com/mp/jaz/home.html'" tppabs="http://www.mapfree.com/mp/jaz/home.html">J. A. Zimmer</A><WBR>
<STRONG>Copyright</STRONG>
<A HREF="copyright.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/copyright.html">Notice</A><WBR>
<P>
<I>Jun 17, 1998</I>
</NOBR></FONT></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -