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 the file for reading and writing.

The file will be truncated if it already exists.

If the named file does not exist, it will be created.

a

Opens the file for writing only.

If the named file does not exist, it will be created.

This will set the file pointer (to the point at which writing will commence) to the end of the file prior to each write.

a+

Opens the file for reading and writing.

If the named file does not exist, it will be created.

Sets the file pointer to the end of the file.

All the legal access in the table may have the character 'b' added as the second or third character (for example wb) to indicate that the open channel should be configured for binary access. Additionally, the file access can be altered after it has been opened with the fconfigure command covered in the next section.

In the second acceptable form, access consists of a list of the following flags, all of which have the standard POSIX meaning. One of the flags must be RDONLY, WRONLY, or RDWR.

Permission

Interpretation

RDONLY

Opens the file for read only.

WRONLY

Opens the file for writing only.

RDWR

Opens the files for read and write access.

APPEND

Sets the file pointer to the end of the file prior to each write.

BINARY

Configures the channel for binary access.

CREAT

Creates the file if it does not exist.

EXCL

If CREAT is also specified, an error is returned if the file exists.

NOCTTY

If the file is a terminal device, this will prevent the file from controlling terminal processes.

NONBLOCK

Prevents the process from blocking while opening the file.

The behavior of this flag is system and device dependant.

TRUNC

If the file exists, it will be truncated.

If a new file is created as part of the opening, permissions (as an integer) are used to set the permissions for the new file in conjunction with the processes' file mode creation. Permissions, by default, provide full access.

Now that we have covered the access types and setting of permissions, let's look at the syntax of the command. The syntax is as follows:

	open filename access permissions

How to do it…

Enter the following command:


% set fp [open text.txt a+]
file5

How it works…

As you can see the open command has returned a file pointer named file5 to our existing file with the permissions set to a+. See the preceding tables for an explanation of the permission notations. Please note that the file pointer is intended to be an opaque value and may not be the same for subsequent invocations.

The open command will open the file referenced by filename with the access type and permissions provided and return a file pointer. Note that we have paired this with the set command to allow access to the file pointer named fp. This is the standard method, as access to the pointer is required to interact with the file.

Bear it in mind that the access types may have hazardous consequences, if utilized improperly. For example, opening a configuration file with the access set to w will result in the file being truncated. This is probably not the method you wish to use for critical data that needs to be retained.

There's more…

Now enter the following command line:


% set fp [open bad.txt RDONLY]
couldn't open "bad.txt": no such file or directory

As we have attempted to access a non-existent file, the Tcl error text for the command was displayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.50.156