Contents
Up
Previous
Next


File types


The file type is used with the file declaration to declare a file object that can be read from and written to. The file type definition specifies what type of objects the file will contain. The syntax for the file type declaration and file declaration is:
file-type-definition : FILE OF type
file-name : expression(string)
file-declaration :
    FILE simple-name ":" subtype IS [ mode ] file-name ";"

The subtype in the file declaration must be a file type. The mode of the file declaration must be IN or OUT indicating if the file can be read or written. If the mode is IN, then during execution the file with the specified file name will be opened for reading. This file must exist in the current directory and must have been written as a file of the same type. If the mode is OUT, then a file is created with the specified file name with the specified type.

If the file declaration appears within a subprogram, then that file is closed when the subprogram exits. Otherwise, it remains open until the end of execution. An object of the file type must be a variable.

The object declared by a file declaration may only be used as a parameter to the built-in read, write, and endfile subprograms. These subprograms are declared as follows

procedure read(f: in FT; value: out TM);
procedure write(f: in FT; value: in TM);
function endfile(f: in FT) return boolean;

The read procedure reads the next value of the appropriate type from a file. When the file is first opened, the next value is the first value in the file. If there are no more values in the file, an error will be reported. The write procedure writes a value of the appropriate type to the end of the file. The endfile function returns true if there are no more values to be read from a file of mode in.

A special package named textio, described in this chapter, provides input and output with text files and the screen and keyboard.