[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 16.4: Indexed and Sequential Files

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 16: Input-Output

16.4 Indexed and Sequential Files

Files are conceived essentially as one-dimensional arrays. Each element in the file has an index value associated with it, the first element having the index value 1. In the case of SEQUENTIAL_IO this index value is hidden from the user, whereas in the case of DIRECT_IO the index value can be directly manipulated.

Considering SEQUENTIAL_IO in more detail, its specification commences:

  type ELEMENT_TYPE is private;
package SEQUENTIAL_IO is
  type FILE_TYPE is limited private;

(The package IO_EXCEPTIONS is explained in section 16.6 below.)

The one generic parameter gives the type of elements in the file. Note that this is private but not limited; this reflects the fact that input-output cannot be performed on limited types, since to do so would indirectly permit assignment; it also forbids attempts to output task values.

The declarations of FILE_TYPE and FILE_MODE then follow. Note that FILE_MODE has only two values - read-write access is not defined for sequential files.

The specification then contains the various file management procedures, CREATE, OPEN, CLOSE, DELETE, and RESET, which were discussed above.

Next come a group of functions which give access to the properties of a file; they are

function MODE (FILE :  in FILE_TYPE) return FILE_MODE;
function NAME (FILE :  in FILE_TYPE) return STRING;
function FORM (FILE :  in FILE_TYPE) return STRING;
function IS_OPEN (FILE :  in FILE_TYPE) return BOOLEAN;

These functions will be found useful when writing general-purpose procedures that manipulate files passed as parameters. They enable the general state of a file to be checked before it is used, and thereby avoid raising exceptions.

Actual input and output is performed by procedures READ and WRITE, which have the specifications:

procedure READ              (FILE :  in FILE_TYPE; ITEM :  out  ELEMENT_TYPE);
procedure WRITE             (FILE :  in FILE_TYPE; ITEM :  in   ELEMENT_TYPE);

Calls of READ and WRITE access the file element at the current position and then move on, ready to access the next element. In other words they increment the hidden index value.

The function

    function END_OF_FILE(FILE :  in FILE_TYPE) return BOOLEAN;

which only applies to files of mode IN_FILE, returns TRUE if there are no more elements to be read.

The package specification concludes with the renaming declarations of various exceptions, as explained in section 16.6 below.

The package DIRECT_IO is very similar. The differences are that the mode now has three values


and there are additional subprograms for direct manipulation of the index. These are

procedure SET_INDEX   (FILE     :  in FILE_TYPE;
                   TO :  in POSITIVE_COUNT);
function INDEX   (FILE :  in FILE_TYPE) return POSITIVE_COUNT;
function SIZE    (FILE :  in FILE_TYPE) return COUNT;


type COUNT is range 0 .. implementation_defined;
subtype POSITIVE_COUNT is COUNT range 1 .. COUNT'LAST;

are declared within DIRECT_IO and specify the type of the index values.

The function INDEX returns the current index value and SET_INDEX enables it to be set. The function SIZE gives the number of items in the file (defined or undefined).

Finally there are overloadings of READ and WRITE with a further parameter giving the index value:

procedure READ     (FILE   :  in     FILE_TYPE;
              ITEM :  out  ELEMENT_TYPE;
              FROM :  in   POSITIVE_COUNT);

procedure WRITE    (FILE   :  in     FILE_TYPE;
               ITEM     :  in   ELEMENT_TYPE;
               TO  :  in   POSITIVE_COUNT);

Thus a call of READ or WRITE can specify a specific index value or, on the other hand, it can be omitted, in which case the normal sequential behavior will occur. This is an illustration of the use of overloading to achieve an effect similar to that of default parameters.

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.