Navigation Logo 2.8  Opening Files Navigation Logo

 

 

To finish the file copying example, we need to open the input and output files. Assuming the input and output files are called input.txt and output.txt respectively, the relevant procedure invocations are

open input.txt r
open output.txt w

The open 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 gets and puts.

Use the open 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.

Remark

There is a more expressive way of specifying file usage by making the second argument a list whose first element is RDONLY, WRONLY, or RDWR for "read only," "write only," and "read-write." Other elements to this list are optional and include: CREAT to create the file if it does not exist, EXCL for use with CREAT if you want an error if the file already exists, APPEND to position the file pointer at the end of the file, and TRUNC to truncate a file to zero length if it exists.

As examples of this more expressive notation, the following two pairs of statements are equivalent.

open input.txt r
open input.txt {RDONLY}

open output.txt w
open output.txt {WRONLY CREAT TRUNC}

Here is the way the input and output files will be opened in the file copying example.

set InFile [open input.txt r]
set OutFile [open output.txt w]
Notice the way command substitution permits you to obtain the proper file identifiers to be assigned to the variables.

Exercise 2.8a

Suppose that variables named InputFileName and OutputFileName contain names of the input and output files, respectively. Rewrite the two file opening statements to use these variables rather than hard-coded names.

Solution

When the Tcl interpreter starts executing, three open files are already opened for you. These are designated stdin, stdout, and stderr. Normally, the stdin takes input from the window that is running Tcl, whereas the stdout and stderr write output there. Use stdout for normal output and stderr for error messages.

Remark

See above in Direct Execution 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.

Tcl also has a close procedure. For example, these commands will close the files opened above

close $InFile 
close $OutFile
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.

Remark

More about input and output statements can be found above in Copying a File Line by Line (gets and puts) and below in The file Action Family (seek), Starting Processes and Pipes (open), and More about I/O ( fconfigure, flush, read, fblocked, and socket).

 

 

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