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:

Optionally, you may read from a file by utilizing the

% 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 ...

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.