Navigation Logo 2.3  Copying a File Line by Line Navigation Logo

 

 

Here, in pseudocode form, is the loop which copies a file line by line from input to output.

while another line is read into Line from input file
   write Line to output file
endwhile

Procedures for reading and writing lines are required for implementing this pseudocode. These procedures are named gets and puts. Both of them work with two arguments where the first argument is a string that identifies an open file. The way to open a file and obtain this identifier is discussed below in Opening Files.

To read a line from a file, use gets, whose first argument is an open-file identifier and whose second argument is the name of the variable to receive the line. If the file identifier is contained in the variable InFile and if the input line is to be placed in the variable Line, then this is the way gets is invoked:

gets $InFile Line
Note that the value of InFile and the name of Line are passed to gets.

The result of executing gets this way is that the next input line of the file referenced by InFile is read to the variable named Line and the number of characters in that line is returned. Any characters which mark the end of the input line are removed and not included in the count of characters. If the end of the file prevents any characters from being read, the value -1 is returned.

To write a line to a file, use puts, whose first argument is an open-file identifier and whose second argument is the string that is to be written. If the file identifer is the value of the variable OutFile and if the string to be written is in the variable Line, then this is the way puts is invoked:

puts $OutFile $Line
The result of executing this procedure is that the value of the variable Line is written to the file referenced by OutFile and then the output line is terminated.

When variables are passed to procedures, their names or their values can be passed. Passing the value of a variable X to a procedure is as easy as writing $X in the argument's position. Variable substitution will arrange for the correct value to be substituted before the argument is passed to the procedure. Passing the name is even easier, just write X in the argument's position. No variable substitution is involved.

Because the purpose of gets' second argument is to name a variable whose value is to be changed, this argument should be a variable name, not a variable value.

Remark

It happens that implementing a procedure so that it can view its parameters as variable names is more difficult than implementing a procedure that views its parameters as simple values. For that reason and for consistency with the way other things are done, we usually pass variable values rather than variable names to Tcl procedures. However, what is usually done cannot always be done.

The difference between gets' second argument and all the other arguments of gets and puts cannot be overemphasized – you are almost certain to use the dollar sign in the wrong place now and then until you get used to what it is for.

Both gets and puts have single argument forms:

  • In gets' single argument form, the argument must be a file identifier and the return value will be the newly read input line. To read from standard input use stdin as the file identifier.

  • In puts' single argument form, the argument is the string to be written. This string will be written to standard output.

Whichever form you use, puts will write an entire output line, that is, it will append an end-of-line character (or characters) to your string. If you do not want puts to do this, then place the switch -nonewline puts before any argument. For example,

puts -nonewline {Hellow Orld}
will write "Hellow Orld" to standard output without finishing the output line.
 

 

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