📄 29.html
字号:
<HTML><TITLE>Basic Syntax and I/O: Completed File Copying Example</TITLE><BODY BGCOLOR="#FFF0E0" VLINK="#0FBD0F" TEXT="#101000" LINK="#0F0FDD">
<A NAME="top"><H1>Completed File Copying Example</H1></A>
<P> Script S2.9a is the completed file copying example.
<P><CENTER><TABLE BORDER>
<CAPTION><ADDR> Script S2.9a: File copying example. </ADDR></CAPTION
<TR><TD><PRE>#!/usr/local/bin/tclsh
## This is script S2.9a of "Tcl/Tk For Programmers"
set InFile [open input.txt r]
set OutFile [open output.txt w]
while {-1 != [gets $InFile Line]} {
<P> puts $OutFile $Line
}
</PRE></TD></TR>
</TABLE></CENTER></P>
<P> <P><A NAME="2.9a">
<STRONG>Exercise 2.9a</STRONG> </A><DL><DD>
Rewrite the file copying example so that it
copies from standard input to standard output.
<P>
<A HREF="2.11.html#Sol2.9a" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.9a">Solution</A></DL>
<P> <P><A NAME="2.9b">
<STRONG>Exercise 2.9b</STRONG> </A><DL><DD>
If you enter an input line with just the number
0, this loop
<PRE>
while "0==[gets stdin]" "puts {another line} "
</PRE>
will stop without printing anything. Why?
If you enter an input line with just the number 1, the same loop will print
"another line" over and over. Why? <P>
<A HREF="2.11.html#Sol2.9b" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.9b">Solution</A></DL>
<P> <P><A NAME="2.9c">
<STRONG>Exercise 2.9c</STRONG> </A><DL><DD>
It is time to start experimenting with
<TT>tclsh.</TT>
<OL>
<P> <P><LI> Start <TT>tclsh??</TT> interactively and type in whatever commands come to
mind to see what happens. Try out variations involving the different kinds of
substitution. You need to become comfortable with this mechanism before
you can understand what happens when Tcl or Tk code executes. Interactive
execution was introduced above in
<A HREF="1.2.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/1.2.html">Interactive Execution</A>.
<P> <P><LI> Execute the file copying example shown in Script S2.9a
interactively.
<P> <P><LI> Execute the file copying example shown in Script S2.9a
directly. Direct execution was introduced above in
<A HREF="1.3.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/1.3.html">Direct Execution</A>.
</OL>
Make sure you successfully complete this exercise before going on. The
material in this book will not be very useful to you if you cannot gain
programming experience as you read.
<P>
<A HREF="2.11.html#Sol2.9c" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.11.html#Sol2.9c">Solution</A></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.8.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.8.html">section</A><WBR>
<STRONG>Next</STRONG>
<A HREF="2.10.html" tppabs="http://www.mapfree.com/sbf/tcl/book/select/Html/2.10.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 + -