Opening a file

The first item we will cover is how to open a file. When you open a file, Tcl creates what it refers to as a "channel" that can be read from and written to. Channels are also created for serial ports, external command pipelines, and when opening sockets.

The Tcl command to open a file is aptly named open. The open command accepts numerous flags to control access and permissions. These are covered in the following:

Access

Interpretation

r

Opens the file for reading only.

The file must already exist.

Default access.

r+

Opens the file for reading and writing.

The file must already exist.

w

Opens the file for writing only.

The file will be truncated if it already exists.

If no named file exists, it will be created.

w+

Opens ...

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.