Closing a file
After any write to a file, you should close it to complete the process. Although exiting a program will close the channel, this is not my preferred manner as a program error may result in the loss of any data that has not been written to disk. To close the file, Tcl provides the close
command. The syntax is as follows:
close channel
How to do it…
Enter the following command:
% set fp [open text.txt a] file5 % puts $fp "Hello Again" % close $fp
How it works…
The close
command flushes the open channel of any pending data resulting in a write to disk and closes the channel. As you can see the close
command has closed the file successfully as there were no errors returned.
To check the file, open it with your text editor of choice. You should see a file that contains the following:
Hello World Hello Again