12 Files


Conceptually similar to most extended Pascals file i/o, Ada provides a simple yet effective collection of file packages. These can be discarded and replaced by any other file package, with of course a subsequent decrease in portability. Most of the facilities provided would not be used in high performance applications directly, but may form the basis of such a file system.

The I/O packages

So far all the I/O performed has been on directed to the standard input/output. All of the facilities provided have come from the TEXT_IO package (even the generic packages integer_io and float_io). This package provides facilities for file manipulation of textual files (files of characters) and provides facilities such as close, delete, reset,open, create etc.

Two other standard I/O packages are for storing files that consist of the one type of fixed length object, such as records, arrays, floating point numbers etc. These are the generic packages sequential_io and direct_io.

The Text_IO package

The text i/o package's main data type is the FILE_TYPE. This is the internal representation of the file. When a file is opened or created an association is made between the name and the FILE_TYPE. The object of type FILE_TYPE is used from then on to reference the file.

The procedure create creates a file and writes some data to it.

The following program reads data from one file and writes it to another, a character at a time. It should be noted that the concept of 'end of line' is different to that provided by Unix and Dos. In those systems there is simply a character that marks the end of the line which is processed as a normal character; in Ada and Pascal there is the concept of a line terminator that is not a character in the file. To read pass this terminator you need to perform a skip_line. Similarly to partition the output file into lines, the command new_line has to be given.

There are various procedures to perform file manipulation. These are

Create - Creates a file with the given name and mode. Note that if the file

- has a null string, then the file is temporary and is deleted later.

Open - Opens an existing file with the given name and mode.

Delete - Deletes the appropriate file. It is an error to delete an open file.

Reset - Returns the read (or write) position to the start of the file.

As well there are functions that report on the status of the file system.

End_of_File - Returns true if we are at the end of the current file.

End_of_Line - Returns true if we are at the end of the current text line.

Is_open - Returns true if the given file is open.

Mode - Returns the mode of the given file.

Name - Returns the name (string) of the current file.

There are various other routines, it is best to examine the specifications of the package text_io (appendix B) to get a clear idea of the package.

Use of text files

Ada's text_io facilities rely on the exception mechanism to report errors in the creation and opening of files. For example attempting to create a file that already exists causes an exception, as does attempting to open a file that does not.

To get around this circularity the following procedure robust_open could be used. This attempts to open the file, if it fails due to the file not being there, it attempts to create it instead.

The code is, of course, subject to race conditions. This program could be interrupted after an attempt open the file, and before the create is attempted; a second process could conceivably create the file in this time.

Another utility, the boolean function file_exists, allows students to check if the file exists. An exception (use_error) is raised if the is already open.

Ada95 Text_io enhancements

The mode append_file has been added to the allowable modes for text files. As well the concept of a standard error file (c.f. Unix/C) has been added. The procedures flush have been added to enable flushing of text file buffers.

Improvements in character handling include look ahead, and get_immediate, with various options.

Generic packages for the I/O of the new modular and decimal types have been included - their specifications conform to that of the other numeric generic I/O packages.

The sequential_io package

Most large systems do not utilise text as the basis for their files. The files are usually composed of composite objects, and typically they are records. The sequential_io generic package allows us to create files whose components are any type (they must however be constrained).

The basics of the sequential_io generic package are identical to the text_io package, except that the procedures get and put are now read and write, and the procedures deal in terms of the type the package was instantiated with. Similarly the concepts of line has dissapeared, so that the function end_of_line and the procedures skip_line and new_line have also gone.

Use of the package is demonstrated below.

No direct access of the file is possible. The file is opened at the start and processed until you get to the end, you reset or close the file.

Ada95 has added the file mode append_file to the specifications of sequential_io.

The direct_io package

The direct io package builds on top of the sequential io package by providing the ability to seek directly to a given record, to determine the size of the file, to determine the current index and to be able to open the file with a new mode - inout_file (read/write).

These facilities should make it possible, in conjuction with a suitable indexing package, to provide very high level file processing packages.

The following code demonstrates the use of direct files. For brevity it assumes that the employee records are stored on the basis of their employee numbers.

to the index...