Navigation Logo 2.9  Completed File Copying Example Navigation Logo

 

 

Script S2.9a is the completed file copying example.

Script S2.9a: File copying example.
#!/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]} {

puts $OutFile $Line }

Exercise 2.9a

Rewrite the file copying example so that it copies from standard input to standard output.

Solution

Exercise 2.9b

If you enter an input line with just the number 0, this loop
while "0==[gets stdin]" "puts {another line} "
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?

Solution

Exercise 2.9c

It is time to start experimenting with tclsh.

  1. Start tclsh?? 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 Interactive Execution.

  2. Execute the file copying example shown in Script S2.9a interactively.

  3. Execute the file copying example shown in Script S2.9a directly. Direct execution was introduced above in Direct Execution.
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.

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.