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

ID 767251
Date 9/08/2022
Public

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

Document Table of Contents

Assign Files to Logical Units

Most I/O operations involve a disk file, keyboard, or screen display. Other devices can also be used:

  • Sockets can be read from or written to if a USEROPEN routine (usually written in C) is used to open the socket.

  • Pipes opened for read and write access block (wait until data is available) if you issue a READ to an empty pipe.

  • Pipes opened for read-only access return EOF if you issue a READ to an empty pipe.

You can access the terminal screen or keyboard by using preconnected files listed in Logical Devices.

You can choose to assign files to logical units by using one of the following methods:

  • Using default values, such as a preconnected unit

  • Supplying a file name (and possibly a directory) in an OPEN statement

  • Using environment variables

Use Default Values

In the following example, the PRINT statement is associated with a preconnected unit (stdout) by default.

PRINT *,100 

The READ statement associates the logical unit 7 with the file fort.7 (because the FILE specifier was omitted) by default:

OPEN (UNIT=7,STATUS='NEW') 
READ (7,100) 

Supply a File Name in an OPEN Statement

The FILE specifier in an OPEN statement typically specifies only a file name (such as filnam) or contains both a directory and file name (such as /usr/proj/filnam).

For example:

OPEN (UNIT=7, FILE='FILNAM.DAT', STATUS='OLD')

The DEFAULTFILE specifier in an OPEN statement typically specifies a pathname that contains only a directory (such as /usr/proj/) or both a directory and file name (such as /usr/proj/testdata).

Implied OPEN

An implied OPEN means that the FILE and DEFAULTFILE specifier values are not specified. If you used an implied OPEN, or if the FILE specifier in an OPEN statement did not specify a file name, you can use an environment variable to specify a file name or a pathname. The pathname can contain both a directory and file name.

Use Environment Variables

You can use shell commands to set the appropriate environment variable to a value that indicates a directory (if needed) and a file name to associate a unit with an external file.

Intel® Fortran recognizes environment variables for each logical I/O unit number in the form of FORTn, where n is the logical I/O unit number. If a file name is not specified in the OPEN statement and the corresponding FORTn environment variable is not set for that unit number, Intel Fortran generates a file name in the form fort.n, where n is the logical unit number.

Implied Intel Fortran Logical Unit Numbers

The ACCEPT, PRINT, and TYPE statements, and the use of an asterisk (*) in place of a unit number in READ and WRITE statements, do not include an explicit logical unit number.

Each of these Fortran statements uses an implicit internal logical unit number and environment variable. Each environment variable is in turn associated by default with one of the Fortran file names that are associated with standard I/O files. The table below shows these relationships:

Intel® Fortran statement

Environment variable

Standard I/O file name

READ (*,f) io-list

FOR_READ

stdin

READ f,io-list

FOR_READ

stdin

ACCEPT f,io-list

FOR_ACCEPT

stdin

WRITE (*,f) io-list

FOR_PRINT

stdout

PRINT f,io-list

FOR_PRINT

stdout

TYPE f,io-list

FOR_TYPE

stdout

WRITE(0,f) io-list

FORT0

stderr

READ(5,f) io-list

FORT5

stdin

WRITE(6,f) io-list

FORT6

stdout

You can change the file associated with these Intel Fortran environment variables, as you would any other environment variable, by means of the environment variable assignment command. For example:

setenv FOR_READ /usr/users/smith/test.dat

After executing the preceding command, the environment variable for the READ statement using an asterisk refers to file test.dat in the specified directory.

NOTE:

The association between the logical unit number and the physical file can occur at runtime. Instead of changing the logical unit numbers specified in the source program, you can change this association at runtime to match the needs of the program and the available resources. For example, before running the program, a script file can set the appropriate environment variable or allow the terminal user to type a directory path, file name, or both.