Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

File Access and File Structure

Fortran supports three methods of file access:

  • Sequential

  • Direct

  • Stream

Fortran supports three kinds of file structure:

  • Formatted

  • Unformatted

  • Binary

Sequential-access and direct-access files can have any of the three file structures. Stream-access files can have a file structure of formatted or unformatted.

Choose a File Access and File Structure

Each kind of file has advantages and the best choice depends on the application you are developing:

  • Formatted Files

    You create a formatted file by opening it with the FORM='FORMATTED' option, or by omitting the FORM parameter when creating a sequential file. The records of a formatted file are stored as ASCII characters. Numbers that would otherwise be stored in binary form are converted to ASCII format. Each record ends with the ASCII carriage return (CR) and/or line feed (LF) characters.

    If you need to view a data file's contents, use a formatted file. You can load a formatted file into a text editor and read its contents directly. The numbers will look like numbers and the strings will look like character strings (an unformatted or binary file looks like a set of hexadecimal characters).

  • Unformatted Files

    You create an unformatted file by opening it with the FORM='UNFORMATTED' option, or by omitting the FORM parameter when creating a direct-access file. An unformatted file is a series of records composed of physical blocks. Each record contains a sequence of values stored in a representation that is close to what is used in program memory. Little conversion is required during input/output.

    The lack of formatting makes these files quicker to access and more compact than files that store the same information in a formatted form. However, if the files contain numbers, you will not be able to read them with a text editor.

  • Binary Files

    You create a binary file by specifying FORM='BINARY'. Binary files are similar to unformatted files, except binary files have no internal record format associated with them.

  • Sequential-Access Files

    Data in sequential files must be accessed in order, one record after the other (unless you change your position in the file with the REWIND or BACKSPACE statements). Some methods of I/O are possible only with sequential files, including nonadvancing I/O, list-directed I/O, and namelist I/O. Internal files must also be sequential files. You must use sequential access for files associated with sequential devices.

    A sequential device is a physical storage device that does not allow explicit motion (other than reading or writing). The keyboard, screen, and printer are all sequential devices.

  • Direct-Access Files

    Data in direct-access files can be read or written to in any order. Records are numbered sequentially, starting with record number 1. All records have the length specified by the RECL option in the OPEN statement. Data in direct-access files is accessed by specifying the record you want within the file. If you need random access I/O, use direct-access files. A common example of a random-access application is a database.

  • Stream-Access Files

    Stream-access I/O is a method of accessing a file without reference to a record structure. With stream access, a file is seen as a continuous sequence of bytes and is addressed by a positive integer starting from 1.

    To enable stream access, specify ACCESS='STREAM' in the OPEN statement for the file. You can use the STREAM= specifier in the INQUIRE statement to determine if STREAM is listed in the set of possible access methods for the file. A value of YES, NO, or UNKNOWN is returned.

    A file enabled for stream access is positioned by file storage units (normally bytes) starting at position 1. To determine the current position, use the POS= specifier in an INQUIRE statement for the unit. You can indicate a required position in a read or write statement with a POS= specifier.

    Stream access can be either formatted or unformatted.

    When connected for formatted stream access, an external file has the following characteristics:

    • The first file storage unit in the file is at position 1. The relationship between positions of successive file storage units is processor dependent; not all positive integers need to correspond to valid positions.

    • Some file storage units may contain record markers that impose a record structure on the file in addition to its stream structure. If there is no record marker at the end of the file, the final record is incomplete. Writing an empty record with no record marker has no effect.

    When connected for unformatted stream access, an external file has the following characteristics:

    • The first file storage unit in the file is at position 1. The position of each subsequent file storage unit is one greater than the position of the preceding file storage unit.

    • If it is possible to position the file, the file storage units do not need to be read or written in order of their position. For example, you may be able to write the file storage unit at position 2, even though the file storage unit at position 1 has not been written.

    • Any file storage unit can be read from the file while it is connected to a unit, if the file storage unit has been written since the file was created, and a READ statement for this connection is allowed.

    • You cannot use BACKSPACE in an unformatted stream.