Using Files

In FORTRAN, we often read our input from the keyboard (READ *) and write our output to the screen (PRINT *). These are known as standard input and standard output, respectively, and they are provided for us automatically. (They are sometimes called "preconnected" files.)

As we progress in programming, we often want to read data from a disk file or write data into a disk file. FORTRAN allows us to do this as well, but the statements used for input and output become a bit more complex.

The sequence of events is:

This is an abbreviated account of using files. There are various parameters and instructions not covered here.


Two important parameters: UNIT and IOSTAT

A file on a disk has a name such as 'thisfile.txt', and if it is not in the same directory, we may need to specify a path to it as well. We need something to represent the file inside the program.

FORTRAN does this by assigning a file a 'UNIT number'. A couple of these are provided automatically; the usual values are: UNIT=5 standard input (keyboard) UNIT=6 standard output (screen)

If we code 'READ *, ...', the '*' indicates we are using the default for input, which is usually unit 5. Likewise, if we code 'PRINT *, ...', we are using the default for output, which is usually unit 6. In all other cases, we will need to specify the value of UNIT, either with UNIT=number or with just the number (in a specific position).

When we do I/O operations, sometimes something can go wrong. For instance, we might try to open a file that doesn't exist, or we might read from a file and find the end of the file instead of more data. For this reason, each I/O operation gives us back a number indicating what happened: 0 for success and some nonzero value for failure. In FORTRAN, we can obtain this value by using an IOSTAT clause:

          IOSTAT=IOResult

Here IOResult is an INTEGER variable.

IOSTAT may be used in any of the input/output statements:


Open a file

The syntax for OPEN is as follows:

         OPEN (UNIT=nn, parameters)
or
         OPEN (nn, parameters)

Here the UNIT number is nn.

Parameters for OPEN:


Closing a file

The syntax for CLOSE is as follows:

         CLOSE (UNIT=nn, parameters)
or
         CLOSE (nn, parameters)

When we are done with a file, we disconnect from its unit using CLOSE. It is possible to OPEN a file, use it, CLOSE it, and then OPEN it again.

Here the UNIT number is nn.

Parameters for CLOSE:


Reading from a file

The syntax for READ is as follows:

         READ (*, format), list             ! standard input, formatted
or
         READ *, list                       ! standard input, default format
or
         READ (*, *) list                   ! standard input, default format
or
         READ (UNIT=nn, parameters) list    ! 1st parameter may be a format
or
         READ (nn, parameters) list         ! 1st parameter may be a format

Here the UNIT number is nn.

This reads data from the file indicated by the unit number. If the unit number is not specified, or if '*' is used, we read from standard input (usually UNIT=5).

Here 'format' may be the label of a FORMAT statement, or it may be a single-quoted format string. In the first format of the READ, this is a positional parameter and must be present; it may be simply '*' (or missing) to represent the default format. (It is also an option to have 'FMT=format'.)

Here 'list' is a list of one or more variables, separated by commas. It may contain an implied DO loop.

The variables in the list will be matched, left to right, with the descriptors in the format.

Parameters for READ:


Writing to a file

The syntax for WRITE is as follows:

         WRITE (UNIT=nn, FMT=format, parameters) list
or
         WRITE (nn, FMT=format, parameters) list
or
         WRITE (UNIT=nn, format, parameters) list
or
         WRITE (nn, format, parameters) list

Here the UNIT number is nn.

This writes data into the file indicated by the unit number. If '*' is used as the unit number, we write into standard output (usually UNIT=6). The unit number and format parameters are mandatory. Here 'FMT=' is optional.

Here 'format' may be the label of a FORMAT statement, or it may be a single-quoted format string. It may be simply '*' to represent the default format.

Here 'list' is a list of one or more variables, separated by commas. It may contain an implied DO loop. If list is omitted, WRITE will apparently write an empty record.

The variables and literals in the list will be matched, left to right, with the descriptors in the format.

After a WRITE, the freshly-written record is the last record in the file. If other records previously existed after its location, they have been lost.

Parameters for WRITE:


Printing

The syntax for PRINT is as follows:

         PRINT format, list

The PRINT command can be understood as a WRITE which always uses standard output (UNIT=6). That is, the above line is equivalent to

         WRITE (*, format) list

Here 'format' and 'list' follow the same rules as with WRITE. If 'list' is omitted, the preceding comma should also be omitted.

If the list is omitted (so we have PRINT *), PRINT will simply print a blank line, single-spaced.