Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.


14.1. External Files and File Objects

[UP][NEXT]

Values input from the external environment of the program, or output to the environment, are considered to occupy external files. An external file can be anything external to the program that can produce a value to be read or receive a value to be written. An external file is identified by a string (the name). A second string (the form) gives further system-dependent characteristics that may be associated with the file, such as the physical organization or access rights. The conventions governing the interpretation of such strings must be documented in Appendix F.

Input and output operations are expressed as operations on objects of some file type, rather than directly in terms of the external files. In the remainder of this chapter, the term file is always used to refer to a file object; the term external file is used otherwise. The values transferred for a given file must all be of one type.

Input-output for sequential files of values of a single element type is defined by means of the generic package SEQUENTIAL_IO. The skeleton of this package is given below.

    with IO_EXCEPTIONS;
    generic   
       type ELEMENT_TYPE is private;
    package SEQUENTIAL_IO is
       type FILE_TYPE is limited private; 

       type FILE_MODE is (IN_FILE, OUT_FILE);
       ...
       procedure OPEN (FILE : in out FILE_TYPE; ...);
       ...  
       procedure READ (FILE : in FILE_TYPE; ITEM : out ELEMENT_TYPE);
       procedure WRITE(FILE : in FILE_TYPE; ITEM : in ELEMENT_TYPE);
       ...
    end SEQUENTIAL_IO; 

In order to define sequential input-output for a given element type, an instantiation of this generic unit, with the given type as actual parameter, must be declared. The resulting package contains the declaration of a file type (called FILE_TYPE) for files of such elements, as well as the operations applicable to these files, such as the OPEN, READ, and WRITE procedures.

Input-output for direct access files is likewise defined by a generic package called DIRECT_IO. Input-output in human-readable form is defined by the (nongeneric) package TEXT_IO.

Before input or output operations can be performed on a file, the file must first be associated with an external file. While such an association is in effect, the file is said to be open, and otherwise the file is said to be closed.

The language does not define what happens to external files after the completion of the main program (in particular, if corresponding files have not been closed). The effect of input-output for access types is implementation-dependent.

An open file has a current mode, which is a value of one of the enumeration types

 type FILE_MODE is (IN_FILE, INOUT_FILE, OUT_FILE);  --  for DIRECT_IO
 type FILE_MODE is (IN_FILE, OUT_FILE);    --  for SEQUENTIAL_IO and TEXT_IO

These values correspond respectively to the cases where only reading, both reading and writing, or only writing are to be performed. The mode of a file can be changed.

Several file management operations are common to the three input-output packages. These operations are described in section 14.2.1 for sequential and direct files. Any additional effects concerning text input-output are described in section 14.3.1.

The exceptions that can be raised by a call of an input-output subprogram are all defined in the package IO_EXCEPTIONS; the situations in which they can be raised are described, either following the description of the subprogram (and in section 14.4), or in Appendix F in the case of error situations that are implementation-dependent.

Notes:

Each instantiation of the generic packages SEQUENTIAL_IO and DIRECT_IO declares a different type FILE_TYPE; in the case of TEXT_IO, the type FILE_TYPE is unique.

A bidirectional device can often be modeled as two sequential files associated with the device, one of mode IN_FILE, and one of mode OUT_FILE. An implementation may restrict the number of files that may be associated with a given external file. The effect of sharing an external file in this way by several file objects is implementation-dependent.

References: create procedure, current index, current size, delete procedure, direct access, direct file procedure, direct_io package, and 14.2, enumeration type, exception, file mode, generic instantiation, index, input file, io_exceptions package, open file, open procedure, output file, read procedure, sequential access, sequential file, sequential input-output, sequential_io package, and 14.2.2, string, text_io package, write procedure.

Rationale references: 16 Input-Output, 16.1 Introduction, 16.2 Basic Requirements, 16.3 Designation of Files

[INDEX][CONTENTS]


[Ada Information Clearinghouse]

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