Input/Output

Almost all FORTRAN 77 programs read data from external sources such as files or the user's terminal, perform calculations on that data, and then write the results to other files or the terminal. FORTRAN 77 provides a powerful and flexible set of features for reading and writing data which are independent of the underlying operating system.

Unit Numbers

Every input or output device is identified by a small, positive integer known as the unit number. These unit numbers are used in READ and WRITE statements to indicates the source or destination for the operation. Two of these numbers are always pre-defined at the start of every FORTRAN 77 program: unit 5 corresponds to standard input, which is often the user's terminal, and unit 6 corresponds to standard output, which is also often the terminal but may be another device, such as a printer.

Example

This example reads a single integer value from standard input and writes it to standard output:

      READ(5,100)I
      WRITE(6,200)I

Unit numbers must be used when reading from or writing to external files. However, when using the standard pre-connected I/O devices, the unit number may be replaced by an asterisk *.

      READ(*,100)I
      WRITE(*,200)I

READ and WRITE Statements

The READ statement reads information from one or more records in a file or standard pre-connected input device (like the terminal) into a data-transfer-list of variables, array elements, etc. Its general form is

READ(UNIT=integer-expression, control-list) data-transfer-list

Correspondingly, the WRITE statement prints information to one or more records in a file or standard pre-connected output device (like the terminal) from a data-transfer-list of variables, array elements, expressions, etc. Its general form is

WRITE(UNIT=integer-expression, control-list) data-transfer-list

The control-list is a set of keyword/value pairs which define the characteristics of the I/O. The unit number must always be given. The following table lists the standard specifiers in FORTRAN 77.

Keyword Description Permitted Values
UNIT The unit number associates the READ or WRITE statement with the input or output device. The unit number is traditionally listed first and if it is, the UNIT= part of the keyword/value pair may be omitted. Any small positive integer expression when referring to an external file or an asterisk * when referring to the standard pre-connected I/O device.
FMT The format specifies how the data in the data-transfer-list is to be arranged. The format value is traditionally listed second after the unit number. If the unit number is listed first and UNIT= is omitted, then the FMT= part of the keyword value pair may also be omitted provided it is listed second. The label of a statement within the same program unit, a character expression or array containing the complete format specification, or an asterisk * for list-directed formatting.
END If a READ statement attempts to input a record beyond the end of the file, an end-of-file condition will be triggered and the program will jump to the statement with the specified label. The label of a statement within the same program unit.
ERR If an error occurs during input or output, the program will jump to the statement with the specified label. The label of a statement within the same program unit.
IOSTAT After the READ or WRITE statement has been executed, the specified variable will contain a status value. This will be zero if the record was input or output successfully. Otherwise, it will be a non-zero value whose meaning is dependent on the operating system. The name of an integer variable or array element with the same program unit.
REC A record number identifier must be used only with direct-access files. An integer expression greater than zero.

Example

      DO 10, I = 1,100
         READ(3,*,END=20,ERR=900)COUNT(I),A(I),NAME(I)
         FILENO = I
   10 CONTINUE
   20 WRITE(*,*)'Input complete. Number of records: ',FILENO
…  
  900 STOP 'Error in input file'
      END

The READ statement is reading in three values from a file associated with the unit number 7 into array elements COUNT(I), A(I) and NAME(I) where I is the loop-control-variable of the enclosing DO loop. If an error occurs during the READ statement, control will be transferred to the statement labelled 900 (which is a STOP statement in this case.) If the end of the file is reached before 300 values are read in, then control will be transferred to the statement labelled 20 which writes a message to the standard output device and the program continues from there.

Formatting

When no specific formatting is specified by the programmer, the computer uses a system-dependent system called list-directed formatting. For output, the formatting depends on the data type of the item and varies from system to system. In general, however, the following rules apply:

As for input, list-directed formatting allows free-format entry for numerical data. The following rules are generally applicable:

Except for CHARACTER data, list-directed input and output files are usually compatible.

It is also possible for the programmer to specify the formatting. The format specification consists of a list of edit descriptors enclosed by a pair of parentheses.

Example

This statement

      WRITE(*,'(1X,3A,F12.2,A)')'The distance to ',STAR(I),' is ',
     $                          DIST(I),' ly'

is equivalent to

      CHARACTER*(*) FORM
      PARAMETER(FORM='(1X,3A,F12.2,A)')
      WRITE(*,FMT=FORM)'The distance to ',STAR(I),' is ',DIST(I),' ly'

which in turn is equivalent to

      WRITE(*,100)'The distance to ',STAR(I),' is ',DIST(I),' ly'
  100 FORMAT(1X,3A,F12.2,A)

If multiple READ or WRITE statements all use the same format, then it is more efficient to use one FORMAT statement for all of them. It is permissible for more than one READ or WRITE statement to reference the same FORMAT statement.

Implied DO loop

The simplest way to input or output all the elements of an array is to simply put the unsubscripted array name in the data transfer list. An implied DO loop allows selective access to array elements and is used in DATA statements as well as READ and WRITE statements. The general form of the implied DO loop is

(data-listloop-control-variable = initial-valuefinal-valuestep-size)

The rules for the initial-value, final-value and step-size are exactly the same as for a DO loop. An implied DO loop may contain other implied DO loops nested within.

Example

      INTEGER   NMAX
      PARAMETER(NMAX=10)
      REAL      TEMPC(NMAX)

      WRITE(*,*)'Enter ',NMAX,' temperatures in Celsius'
      READ(*,*)(TEMPC(I),I=1,NMAX)
      WRITE(*,100)(TEMPC(I),I=1,NMAX)
  100 FORMAT(' ',5F8.2)
      STOP 'End of program'
      END

After printing out a message telling the user to input NMAX values, the program uses an implied DO loop to read those values into the array TEMPC. Then the values are output to the standard output device using an implied DO loop in a formatted WRITE statement.

Nested implied DO loops are particularly useful when dealing with multi-dimensional arrays.

Example

      WRITE(*,100)((MATRIX(I,J),J=1,8),I=1,5)

Using a FORMAT statement labelled 100, the WRITE statement prints out 40 array elements in the order MATRIX(1,1), MATRIX(1,2), …, MATRIX(1,8), MATRIX(2,1), MATRIX(2,2), …, MATRIX(2,8), …, MATRIX(5,1), MATRIX(5,2), …, MATRIX(5,8)

Simplified READ and PRINT Statements

The simplified READ statement

READ format, data-transfer-list

reads in list-directed or formatted input from the standard input device and is directly equivalent to

READ(*,format)data-transfer-list

The equivalent statement for WRITE is

PRINT format, data-transfer-list

which writes out list-directed or formatted output to the standard output device. It is directly equivalent to

WRITE(*,format)data-transfer-list

There is nothing to recommend the simplified versions of READ and PRINT. Use the standard READ and WRITE statements in their place.

Opening a File

Files are associated with specific unit numbers using the OPEN statement. The general form of this statement is:

OPEN(UNIT=integer-expression, control-list)

The control-list is a set of keyword/value pairs which define the characteristics of the file to be opened. The unit number must always be given. The file name also must be given except in the case of scratch files. The following table lists the standard specifiers in FORTRAN 77.

Keyword Description Permitted Values
UNIT The unit number is associated with the file from the time it is opened until it is closed. The unit number is traditionally listed first and if it is, the UNIT= part of the keyword/value pair may be omitted. Any small positive integer expression.
FILE The name of the file which is to be associated with this unit. Any valid file name, as a character string.
STATUS This is used to specify whether the file must already exist, or must not exist, or whether it is a temporary (scratch) file. 'OLD' (the file must already exist), 'NEW' (the file must not exist), 'UNKNOWN' (the file may or may not exist), or 'SCRATCH' (the file is a scratch file). No other values are allowed.
The default is 'UNKNOWN'.
ERR If an error occurs whilst opening the file, the program will jump to the statement with the specified label. The label of a statement within the same program unit.
IOSTAT After the OPEN statement has been executed, the specified variable will contain a status value. This will be zero if the file was opened successfully. Otherwise, it will be a non-zero value whose meaning is dependent on the operating system. The name of an integer variable or array element within the same program unit.
FORM Whether the file is to be used for formatted (plain text) or unformatted (binary) I/O. 'FORMATTED' or 'UNFORMATTED'. No other values are allowed.
The default is 'FORMATTED'.
ACCESS Whether the file is to be used for sequential or random I/O. 'SEQUENTIAL' or 'DIRECT'. No other values are allowed.
The default is 'SEQUENTIAL'.
RECL The record length for direct access files. An integer which defines the record length.
BLANK Controls how blanks are to be interpreted in formatted numeric fields. 'NULL' (blanks are to be ignored) or
'ZERO' (blanks are to be treated as if they were zeros). No other values are allowed.
The default is 'NULL'.

Example

  100 WRITE(*,*)'Enter the input file name'
      READ(*,'(A)',END=999)FNAME
      OPEN(13,FILE=FNAME,STATUS='OLD',BLANK='ZERO',ERR=100)

Unit number 13 will be associated with the file whose name is input by the user. If there is an error opening the file (perhaps it doesn't exist or there is some other problem), then control will transfer to the statement labelled 100, prompting the user to input a different name. If the file is opened successfully, blanks will treated as if they were zeros. Note that if the user enters the (system-dependent) end-of-file character during the READ statement, control will transfer to a statement labelled 999.

Scratch Files

FORTRAN 77 provides an easy way to open a temporary file to act as scratch storage for a program. Consider the following example:

Example

      OPEN(8,STATUS='SCRATCH')

Unit number 8 will be associated with a temporary unnamed file which can be used for I/O in exactly the same way as any other file. However, after the file is closed, or if the program terminates without explicitly closing the file, the scratch file will be deleted automatically by the FORTRAN 77 I/O subsystem.

Note that no name is specified for the file in the OPEN statement. Indeed, it is forbidden to specify a file name for a scratch file.

Closing a File

When a file is no longer required by the program, it should be closed. This breaks the association between the file and its unit number. The general form of this statement is

CLOSE(UNIT=integer-expression, control-list)

The control-list is a set of keyword/value pairs which define the how the file is to be closed. The unit number must always be given. The following table lists the standard specifiers in FORTRAN 77.

Keyword Description Permitted Values
UNIT The unit number is associated with the file from the time it is opened until it is closed. The unit number is traditionally listed first and if it is, the UNIT= part of the keyword/value pair may be omitted. Any small positive integer expression.
STATUS This is used to specify whether the file should be kept or deleted after being closed. 'KEEP' (the file should be kept after closing) or 'DELETE' (the file should be deleted). No other values are allowed.
The default is 'KEEP', except for scratch files, which are always deleted after being closed.
ERR If an error occurs whilst closing the file, the program will jump to the statement with the specified label. The label of a statement within the same program unit.
IOSTAT After the CLOSE statement has been executed, the specified variable will contain a status value. This will be zero if the file was closed successfully. Otherwise, it will be a non-zero value whose meaning is dependent on the operating system. The name of an integer variable or array element within the same program unit.

Example

      CLOSE(27,STATUS='DELETE')
      CLOSE(39)

The file associated with unit number 27 is deleted after being closed. The file associated with unit number 39 is kept after being closed unless it was opened as a scratch file in which case it is automatically deleted.

INQUIRE Statement

The INQUIRE statement is useful when you wish to learn more about a file, such as whether it exists or if it is already connected. This statement takes on two slightly different forms. If you wish to determine whether a unit number is already in use and the characteristics of the file associated with it, then you use the inquire by unit form for the statement:

INQUIRE(UNIT=integer-expression, inquire-list)

If no file is connected to the specified unit number, then most of the arguments in the inquire-list will be undefined or return 'UNKNOWN' as their values.

The inquire by file form of the statement can be used to find out whether or not a named file exists.

INQUIRE(FILE=character-expression, inquire-list)

You may inquire by unit or inquire by file but not both in the same INQUIRE command.

The inquire-list is a set of keyword/value pairs which return values to the named variables or array elements. (The only exception is ERR=label where label is the label of a statement within the same program unit.) The following table lists the standard specifiers in FORTRAN 77.

Keyword Description Permitted Values
UNIT The unit number is associated with the file from the time it is opened until it is closed. The UNIT= part of the keyword/value pair may be omitted in the inquire by unit statement.
Either UNIT or FILE must be used but not both.
Any small positive integer expression.
FILE The name of the file which is to be associated with this unit. Trailing blanks in the file name are ignored and the file need not be connected to a unit in the program.
Either UNIT or FILE must be used but not both.
The name of a character variable or array element within the same program unit.
ERR If an error occurs whilst executing the INQUIRE command, the program will jump to the statement with the specified label. This does not infer that there is an error with the unit number or file. The label of a statement within the same program unit.
IOSTAT After the INQUIRE statement has been executed, the specified variable will contain a status value. This will be zero if the command was executed successfully. Otherwise, it will be a non-zero value whose meaning is dependent on the operating system. This does not infer that there is an error with the unit number or file. The name of an integer variable or array element within the same program unit.
EXIST The variable is set to .TRUE. if the specified unit|file exists and .FALSE. otherwise. The name of a logical variable or array element within the same program unit.
OPENED The variable is set to .TRUE. if the specified unit|file is connected to a file|unit in the program and .FALSE. otherwise. The name of a logical variable or array element within the same program unit.
NAMED The variable is set to .TRUE. if the file has a name and .FALSE. otherwise. The name of a logical variable or array element within the same program unit.
NAME The variable returns the file name if the file has a name; otherwise it is undefined. If a name is returned, it is suitable for use in the OPEN statement. The name of a character variable or array element within the same program unit.
NUMBER The variable returns the unit number of the file which is connected. If no file is connected, the variable is undefined. This specifier cannot be used in the inquire by unit statement. The name of an integer variable or array element within the same program unit.
ACCESS The variable returns 'SEQUENTIAL' if the connection is for sequential I/O or 'DIRECT' if the connection is for direct I/O. The value is undefined if there is no connection. The name of a character variable or array element within the same program unit.
DIRECT The variable returns 'YES' if the file can be connected for direct I/O, 'NO' if it can't, and 'UNKNOWN' if the system can't tell. The name of a character variable or array element within the same program unit.
SEQUENTIAL The variable returns 'YES' if the file can be connected for sequential I/O, 'NO' if it can't, and 'UNKNOWN' if the system can't tell. The name of a character variable or array element within the same program unit.
FORM The variable returns 'FORMATTED' if the connection is for formatted I/O or 'UNFORMATTED' if the connection is for unformatted I/O. The value is undefined if there is no connection. The name of a character variable or array element within the same program unit.
FORMATTED The variable returns 'YES' if the file can be connected for formatted I/O, 'NO' if it can't, and 'UNKNOWN' if the system can't tell. The name of a character variable or array element within the same program unit.
UNFORMATTED The variable returns 'YES' if the file can be connected for unformatted I/O, 'NO' if it can't, and 'UNKNOWN' if the system can't tell. The name of a character variable or array element within the same program unit.
RECL The variable returns the record length if the file is connected for direct-access and is undefined otherwise. The record length is the number of characters in a formatted file but the units are system-dependent for unformatted files. The name of an integer variable or array element within the same program unit.
NEXTREC The variable returns the value n + 1 where n is the record number of the last record transferred to/from a direct-access file. If no records have been transferred, then the value 1 is returned. The value is undefined otherwise. The name of an integer variable or array element within the same program unit.
BLANK The variable returns 'NULL' if null blank control is in effect for the file connected for formatted I/O or 'ZERO' if blanks are being converted to zeros. The value is undefined if there is no connection. The name of a character variable or array element within the same program unit.

Example

Suppose we need to open a named file within a subroutine, but do not know which unit numbers are available. We can use INQUIRE to find the smallest unit number that is not currently in use. This simple function searches all unit numbers within a specified range, and returns the smallest number which does not already have an open file associated with it. If all unit numbers in the range are in use, the function returns the special value -1.

      INTEGER FUNCTION IFREE(IFIRST,ILAST)
      INTEGER IFIRST,ILAST,IUNIT
      LOGICAL ISOPEN

      DO 10, IUNIT=IFIRST,ILAST
         INQUIRE(UNIT=IUNIT, OPENED=ISOPEN)
         IF (.NOT.ISOPEN) THEN
            IFREE = IUNIT
            RETURN
         END IF
   10 CONTINUE
      IFREE = -1
      END

Unformatted I/O

When reading or writing very large data sets, it is often more efficient to store the data in the host machine's native binary format rather than in human-readable format. FORTRAN 77 provides unformatted I/O for this purpose. Unformatted files are generally more compact and can be read and written much more quickly, because there is no need for the computer to convert between human-readable text and its native binary format.

However, unformatted files cannot be opened with a text editor. They can only be read by a FORTRAN 77 program.

Example

Suppose we run the following short program:

      PROGRAM XAMPLE
      DOUBLE PRECISION D
      INTEGER          I,J

      I = 1024*1024
      J = -1
      D = 10.0D0

      OPEN(8,FILE='xample.out',STATUS='NEW',FORM='UNFORMATTED')
      WRITE(8)I,J,D
      CLOSE(8,STATUS='KEEP')
      STOP 'End of program'
      END

On an Intel-based Linux machine, this produces a file named xample.out which is 24 bytes long. When we examine its contents using the hd (hexadecimal dump) utility, we see this:

00000000  10 00 00 00 00 00 10 00  ff ff ff ff 00 00 00 00  |................|
00000010  00 00 24 40 10 00 00 00                           |..$@....|

The first four bytes are a record length, generated automatically by the FORTRAN 77 I/O subsystem. It is in little-endian byte order, so the value in decimal is 16. Then follow the actual data which the program wrote: the two 32-bit integers in little-endian byte order, and the IEEE754 double-precision (64-bit) number. Finally, the record length is written again by the FORTRAN 77 I/O subsystem as a safeguard.

Sequential I/O

Most programs use sequential I/O. They open a data file, and read its contents from start to finish, processing the values as they are read, and writing results to an output file.

REWIND Statement

It is possible to re-read a sequential input file from the first record by using the REWIND command. The name is a reminder that FORTRAN originates from an era when magnetic tapes were the most common mass-storage medium. The REWIND command once did exactly what its name implies: it caused a magnetic tape to be rewound to the start, so that the data on it could be re-read.

The general form of this statement is

REWIND(UNIT=integer-expression, control-list)

The control-list must contain the UNIT specifier and may contain the ERR and IOSTAT specifiers, which have exactly the same syntax and meaning as their counterparts in the OPEN and CLOSE statements. As always, if the unit number is listed first in the control-list, the UNIT= part of the keyword/value pair may be omitted.

BACKSPACE Statement

If the program needs to re-read only the most recently read (or written) record, then the BACKSPACE statement can be used. The general form of this statement is

BACKSPACE(UNIT=integer-expression, control list)

The control list may contain the same specifiers as the REWIND statement.

Direct I/O

Some programs may need to access records in a data file in a non-sequential manner. Consider, for example, a data file which contains customer information, with one record per customer. It would be inefficient to read the entire file in order to obtain the data on a single customer. Instead, the program should be able to skip all of the intervening records, and read only the record for that customer. FORTRAN 77 provides direct-access I/O for this purpose.

In order to make direct-access I/O efficient, FORTRAN 77 mandates that all records in a direct-access file must be exactly the same length. This allows the FORTRAN 77 I/O subsystem to determine the offset of the desired record in the file by performing a simple arithmetic calculation. It is the programmer's responsibility to specify the correct record length when opening the file.

Direct-access I/O is normally carried out on unformatted files, although it can also be used with formatted files, if sufficient caution is used.

Example

Suppose we run the following short program:

      PROGRAM XAMPLE
      DOUBLE PRECISION D
      INTEGER          I,J

      I = 1024*1024
      J = -1
      D = 10.0D0

      OPEN(8,FILE='xample.out',STATUS='NEW',FORM='UNFORMATTED',
     $     ACCESS='DIRECT',RECL=16)
      WRITE(8,REC=1)I,J,D
      CLOSE(8,STATUS='KEEP')
      STOP 'End of program'
      END

On an Intel-based Linux machine, this produces a file named xample.out which is 16 bytes long. When we examine its contents using the hd (hexadecimal dump) utility, we see this:

00000000  00 00 10 00 ff ff ff ff  00 00 00 00 00 00 24 40  |..............$@|

The file contains a single 16-byte record comprising the two 32-bit integers and the 64-bit floating point number.

FORTRAN 77 allows us to write to any record in a direct-access file. We do not have to write the records sequentially. We can open a new file and write record number 3 without having to write records 1 and 2.

Example

If we modify the previous program slightly, changing the WRITE statement to

WRITE(8,REC=3)I,J,D

and then re-compile and re-run the program, the output file is now 48 bytes long, and its contents, displayed by the hd utility, look like this:

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 10 00 ff ff ff ff  00 00 00 00 00 00 24 40  |..............$@|

Notice that there are three 16-byte records in the file, but the first two are filled with nulls. Only record number 3 contains valid data.

Internal Files

The files discussed above are all external files. FORTRAN 77 also allows computer memory to be used as if it was an external file. This internal file exists only whilst the program is executing and behaves like a formatted sequential file.

An internal file is a CHARACTER object such as a constant, variable, substring, array or array element, and is most often used for converting between CHARACTER and other data types. It is accessed only with READ and WRITE statements with explicit format specifications. However, instead of a number, the unit in the READ or WRITE statement must be an object of type CHARACTER.

Example

Consider the following program fragment. We wish to use an internal file to assign a value to the CHARACTER variable TITLE.

      CHARACTER*40 TITLE
      REAL         STIME
 
   10 CONTINUE
         WRITE(*,*)'Enter the decimal sidereal time'
         READ(*,*,END=999)STIME
      IF (STIME .LT. 0.0 .OR. STIME .GT. 24.0) GOTO 10
      WRITE(TITLE,100)INT(STIME),NINT((STIME-INT(STIME))*60)
  100 FORMAT('The sidereal time is ',I2,' hours ',I2,' minutes')

The second WRITE statement uses the FORMAT statement labelled 100 to write text and integers to the CHARACTER variable TITLE. If STIME is initialised to 15.6, the TITLE contains the value

Thesiderealtimeis15hours36minutes

where is a blank.

Example

Consider the following program fragment where we have information stored in a CHARACTER variable but wish to convert it to another type (in this case INTEGER) by means of an internal file.

      CHARACTER*10 NUMBER
      INTEGER      I,J

      NUMBER = '1 2 3 4 5 '
      READ(NUMBER,'(2I5)')I,J      

The CHARACTER variable contains the value 12345 where is a blank. The READ statement uses an internal file to convert this value using the format specification (2I5) into two variables of type INTEGER. The first 5 places are placed in the variable I and the second 5 places are placed in J. As a result, I contains the value 123 and J contains 45. (The blanks are ignored.)

However, if we change the READ statement to

      READ(NUMBER,'(BZ,2I5)')I,J      

I takes the value 10203 and J becomes 04050. This is because the descriptor BZ forces blanks to be treated as zeros.

The statements BACKSPACE and REWIND may be used with internal files but no other I/O commands are permitted.