FORTRAN 77 Language Reference

Essential FORTRAN I/O Concepts

Any operating system based on the UNIX operating system is not as record-oriented as FORTRAN. This operating system treats files as sequences of characters instead of collections of records. The FORTRAN runtime system keeps track of file formats and access mode during runtimes. It also provides the file facilities, including the FORTRAN libraries and the standard I/O library.

Logical Units

The FORTRAN default value for the maximum number of logical units that a program can have open at one time is 64. For current Solaris releases, this limit is 256. A FORTRAN program can increase this limit beyond 64 by calling the setrlim() function. See the man page setrlim(2). If you are running csh, you can also do this with the limit or unlimit command; see csh(1).

The standard logical units 0, 5, and 6 are preconnected as stderr, stdin, and stdout, respectively. These are not actual file names, and cannot be used for opening these units. INQUIRE does not return these names, and indicates that the above units are not named unless they have been opened to real files. However, these units can be redefined with an OPEN statement.

The names, stderr, stdin, and stdout, are meant to make error reporting more meaningful. To preserve error reporting, the system makes it an error to close logical unit 0, although it can be reopened to another file.

If you want to open a file with the default file name for any preconnected logical unit, remember to close the unit first. Redefining the standard units can impair normal console I/O. An alternative is to use shell redirection to externally redefine the above units.

To redefine default blank control or the format of the standard input or output files, use the OPEN statement, specifying the unit number and no file name, and use the options for the kind of blank control you want.

I/O Errors

Any error detected during I/O processing causes the program to abort, unless alternative action has been provided specifically in the program. Any I/O statement can include an ERR= clause (and IOSTAT= clause) to specify an alternative branch to be taken on errors and return the specific error code. Read statements can include END=n to branch on end-of-file. File position and the value of I/O list items are undefined following an error. END= catches both EOF and error conditions; ERR= catches only error conditions.

If your program does not trap I/O errors, then before aborting, an error message is written to stderr with an error number in square brackets, [ ], and the logical unit and I/O state. The signal that causes the abort is IOT.

Error numbers less than 1000 refer to operating system errors; see intro(2). Error numbers greater than or equal to 1000 come from the I/O library.

For external I/O, part of the current record is displayed if the error was caused during reading from a file that can backspace. For internal I/O, part of the string is printed with a vertical bar (|) at the current position in the string.

General Restriction

Do not reference a function in an I/O list if executing that function causes an I/O statement to be executed. Example:


WRITE( 1, 10)  Y, A + 2.0 * F(X)   !  Wrong if F() does I/O

Kinds of I/O

The four kinds of I/O are: formatted, unformatted, list-directed, and NAMELIST.

The two modes of access to files are sequential and direct. When you open a file, the access mode is set to either sequential or direct. If you do not set it explicitly, you get sequential by default.

The two types of files are: external files and internal files. An external file resides on a physical peripheral device, such as disk or tape. An internal file is a location in main memory, is of character type, and is either a variable, substring, array, array element, or a field of a structured record.

Combinations of I/O

I/O combinations on external files are:


Not Allowed 

Sequential unformatted Sequential formatted Sequential list-directed Sequential NAMELIST Direct unformatted Direct formatted

Direct-access, list-directed I/O Direct-access, NAMELIST I/O NAMELIST I/O on internal files Unformatted, internal I/O

The following table shows combinations of I/O form, access mode, and physical file types.

Table 5-1 Summary of f77 Input and Output

Kind of I/O 

Access Mode: 


File Type 





The file is a character variable, substring, array, or array element. @

The file is a character array; each record is one array element.  


Only formatted records of same or variable length. 

Only formatted records, all the same length.  



Not allowed. 

Not allowed. 


Contains only unformatted records. 

READ: Gets one logical record at a time. WRITE: Unfilled part of record is undefined.



READ: Reads characters until EOF or I/O list is satisfied.


WRITE: Writes records until list is satisfied. @

Not allowed. 


Uses standard formats based on type of variable and size of element. Blanks or commas are separators. Any columns.  

Not allowed. 



Not allowed. 

Not allowed. 


READ: Reads records until it finds $groupname in columns 2-80. Then reads records searching for names in that group, and stores data in those variables. Stops reading on $ or eof.


WRITE: Writes records showing the group name and each variable name with value.

Not allowed. 

Avoid list-directed internal writes. The number of lines and items per line varies with the values of items.

Printing Files

You get a print file by using the nonstandard FORM='PRINT' in OPEN. @

	OPEN ( ..., FORM='PRINT', ... )

This specifier works for sequential access files only.


A print file has the following features:

In general, if you open a file with FORM='PRINT', then for that file list-directed output does not provide the FORTRAN Standard blank in column one; otherwise, it does provide that blank. FORM='PRINT' is for one file per call.

If you open a file with FORM='PRINT', then that file has the same content as if it was opened with FORM='FORMATTED', and filtered with the output filter, asa.

If you compile with the -oldldo option (old list-directed output), then all the files written by the program do list-directed output without that blank in column one; otherwise, they all get that blank. The -oldldo option is global.

The INQUIRE Statement

The INQUIRE statement returns 'PRINT' in the FORM variable for logical units opened as print files. It returns -1 for the unit number of an unopened file.

Special Uses of OPEN

If a logical unit is already open, an OPEN statement using the BLANK option does nothing but redefine that option.

As a nonstandard extension, if a logical unit is already open, an OPEN statement using the FORM option to switch between FORM='PRINT' and FORM='FORMATTED' does nothing but redefine that option. @

These forms of the OPEN statement need not include the file name, and must not include a file name if UNIT refers to standard input, output, or standard error.

If you connect a unit with OPEN and do not use the file name parameter, then you get the default file name, fort.nn, where nn is the unit number. Therefore, to redefine the standard output as a print file, use:


Scratch Files

Scratch files are temporary files that normally disappears after execution is completed.

Example: Create a scratch file:


To prevent a temporary file from disappearing after execution is completed, you must execute a CLOSE statement with STATUS='KEEP'. KEEP is the default status for all other files.

Example: Close a scratch file that you want to access later:


Remember to get the real name of the scratch file. Use INQUIRE if you want to reopen it later.

Changing I/O Initialization with IOINIT

Traditional FORTRAN environments usually assume carriage control on all logical units. They usually interpret blank spaces on input as zeroes, and often provide attachment of global file names to logical units at runtime. The routine IOINIT(3F) can be called to specify these I/O control parameters. This routine:

Example: IOINIT and logical unit preattachment:


For the above call, the FORTRAN runtime system looks in the environment for names of the form FORTnn, and then opens the corresponding logical unit for sequential formatted I/O.

With the above example, suppose your program opened unit 7, as follows:


The FORTRAN runtime system looks in the environment for the FORT07 file, and connects it to unit 7.

In general, names must be of the form PREFIXnn, where the particular PREFIX is specified in the call to IOINIT, and nn is the logical unit to be opened. Unit numbers less than 10 must include the leading 0. For details, see IOINIT(3F) and the Sun Fortran Library Reference.

Example: Attach external files ini1.inp and ini1.out to units 1 and 2:

In sh:

demo$ TST01=ini1.inp 
demo$ TST02=ini1.out 
demo$ export TST01 TST02 

In csh:

demo% setenv TST01 ini1.inp
demo% setenv TST02 ini1.out

Example: Attach the files, ini1.inp and ini1.out, to units 1 and 2:

demo% cat ini1.f 
& 		/.TRUE., .FALSE., .FALSE., 'TST', .FALSE. / 
	READ( 1, *) I, B, N 
	WRITE( *, *) 'I = ', I, ' B = ', B, ' N = ', N 
	WRITE( 2, *) I, B, N 
demo% cat $TST01
 12 3.14159012 6
demo% f77 ini1.f
demo% a.out 
 I =   12 B =     3.14159 N =   6
demo% cat $TST02
   12    3.14159  6

IOINIT should prove adequate for most programs as written. However, it is written in FORTRAN so that it can serve as an example for similar user-supplied routines. A copy can be retrieved as follows:

demo% cp /opt/SUNWspro/SC5.0/src/ioinit.f  .