DIGITAL Fortran 90
User Manual for
DIGITAL UNIX Systems


Previous Contents Index

  1. The current directory is used and the unit number determines the file name.
  2. The current directory is used and the environment variable provides the file name.
  3. The environment variable provides both the directory and file name.
  4. The directory is provided by the OPEN DEFAULTFILE specifier value, and the unit number determines the file name.
  5. The directory is provided by the OPEN DEFAULTFILE specifier value, and the environment variable provides the file name.
  6. The directory is provided by the OPEN DEFAULTFILE specifier value, and the environment variable provides a subdirectory and file name.
  7. The directory is provided by the OPEN DEFAULTFILE specifier value, and the file name is provided by the OPEN FILE specifier value.
  8. The directory and file name are provided by the OPEN FILE specifier value.
  9. The current directory is used and the OPEN FILE specifier value provides the file name.

When the resulting file pathname begins with a tilde character (~), C shell style pathname substitution is used (regardless of what shell is being used), such as a top-level directory (below the root). For additional information on tilde pathname substitution, see csh(1).

Rules for Applying Default Pathnames and File Names

DIGITAL Fortran 90 determines file name and the directory path based on certain rules. It determines a file name string as follows:

Once DIGITAL Fortran 90 determines the resulting file name string, it determines the directory (which optionally precedes the file name) as follows:

7.5.1.6 Coding File Locations in an OPEN Statement

You can use the FILE and DEFAULTFILE specifiers of the OPEN statement to specify the complete definition of a particular file to be opened on a logical unit. (The DIGITAL Fortran Language Reference Manual describes the OPEN statement in greater detail.) For example:


OPEN (UNIT=4, FILE='/usr/users/smith/test.dat', STATUS='OLD') 

The file test.dat in directory /usr/users/smith is opened on logical unit 4. No defaults are applied since both the directory and file name were specified. The value of the FILE specifier can be a character constant, variable, or expression.

In the following interactive example, the user supplies the file name and the DEFAULTFILE specifier supplies the default values for the full pathname string. The file to be opened is in /usr/users/smith and is concatenated with the file name typed by the user into the variable DOC:


     CHARACTER(LEN=9) DOC 
     WRITE (6,*)  'Type file name ' 
     READ (5,*) DOC 
     OPEN (UNIT=2, FILE=DOC, DEFAULTFILE='/usr/users/smith',STATUS='OLD') 

A slash is appended to the end of the default file string if it does not have one.

For an example program that reads a typed file name, uses the typed name to open a file, and handles such errors as the "file not found" error, see Example 8-1.

7.5.1.7 Using Environment Variables

You can use the environment variable mechanism of the operating system and shells to associate logical units with external files. For example, setting the environment variable FORT6 to a file lets you redirect stdout to the specified file (see Table 7-4).

DIGITAL Fortran 90 attempts to use certain environment variables in the absence of a file name.

When using scratch files, you can use the TMPDIR environment variable to specify where the scratch file gets created (see Section 7.4.2).

Setting and Unsetting Environment Variables

Before program execution, you can use shell commands to specify a value for an environment variable. This specified value might be a directory and/or file name of an external file you want to associate with a preconnected unit or a specific unit number.

With the C Shell, use the setenv command to set an environment variable:


% setenv FORT8 /usr/users/smith/test.dat

To remove the association of an environment variable and an external file with the C shell, use the unsetenv command.


% unsetenv FORT8

With the Bourne shell (sh) and Korn shell (ksh), use the export command and assignment command to set the environment variable:


$ export FORT8
$ FORT8=/usr/users/smith/test.dat

To remove the association of an environment variable and an external file with the Bourne or Korn shell, use the unset command:


$ unset FORT8

Implied DIGITAL Fortran 90 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 90 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 90 file names that are associated with standard I/O files. Table 7-7 shows these relationships.

Table 7-7 Implicit DIGITAL Fortran 90 Logical Units
DIGITAL Fortran 90 Statement Environment Variable When -vms Specified Environment Variable When -vms Omitted Standard I/O File Name
READ (*,f) iolist FOR_READ FORT5 stdin
READ f,iolist FOR_READ FORT5 stdin
ACCEPT f,iolist FOR_ACCEPT FORT5 stdin
WRITE (*,f) iolist FOR_PRINT FORT6 stdout
PRINT f,iolist FOR_PRINT FORT6 stdout
TYPE f,iolist FOR_TYPE FORT6 stdout

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


% 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 directory /usr/users/smith .

For More Information:

7.5.2 Obtaining File Information: The INQUIRE Statement

The INQUIRE statement returns information about a file and has three forms:

7.5.2.1 Inquiry by Unit

An inquiry by unit is usually done for an opened (connected) file. An inquiry by unit causes the DIGITAL Fortran 90 RTL to check whether the specified unit is connected or not. One of the following occurs:

For example, the following INQUIRE statement shows whether unit 3 has a file connected (OPENED specifier) in logical variable I_OPENED, the name (case sensitive) in character variable I_NAME, and whether the file is opened for READ, WRITE, or READWRITE access in character variable I_ACTION:


  INQUIRE (3, OPENED=I_OPENED, NAME=I_NAME, ACTION=I_ACTION) 

7.5.2.2 Inquiry by File Name

An inquiry by name causes the DIGITAL Fortran 90 RTL to scan its list of open files for a matching file name. One of the following occurs:

The following INQUIRE statement returns whether the file named log_file is a file connected in logical variable I_OPENED, whether the file exists in logical variable I_EXIST, and the unit number in integer variable I_NUMBER.


  INQUIRE (FILE='log_file', OPENED=I_OPEN, EXIST=I_EXIST, NUMBER=I_NUMBER) 

7.5.2.3 Inquiry by Output Item List

Unlike inquiry by unit or inquiry by name, inquiry by output item list does not attempt to access any external file. It returns the length of a record for a list of variables that would be used for unformatted WRITE, READ, and REWRITE statements (REWRITE is a DIGITAL Fortran extension).

The following INQUIRE statement returns the maximum record length of the variable list in variable I_RECLENGTH. This variable is then used to specify the RECL value in the OPEN statement:


  INQUIRE (IOLENGTH=I_RECLENGTH) A, B, H 
  OPEN (FILE='test.dat', FORM='UNFORMATTED', RECL=I_RECLENGTH, UNIT=9) 

For an unformatted file, the RECL value is returned using 4-byte units, unless you specify the -assume byterecl option to request 1-byte units.

For More Information:

7.5.3 Closing a File: The CLOSE Statement

Usually, any external file opened should be closed by the same program before it completes. The CLOSE statement disconnects the unit and its external file. You must specify the unit number (UNIT specifier) to be closed.

You can also specify:

To delete a file when closing it:

If you opened an external file and did an inquire by unit, but do not like the default value for the ACCESS specifier, you can close the file and then reopen it, explicitly specifying the ACCESS desired.

There usually is no need to close preconnected units. Internal files are neither opened nor closed.

For More Information:

7.6 Record Operations

After you open a file or use a preconnected file, you can use the following statements:

These statements are described in Section 7.2 and the DIGITAL Fortran Language Reference Manual.

The record I/O statement must use the appropriate record I/O form (formatted, list-directed, namelist, or unformatted), as described in Section 7.3.

7.6.1 Record I/O Statement Specifiers

You can use the following specifiers with the READ and WRITE record I/O statements:

When using nonadvancing I/O, use the ADVANCE, EOR, and SIZE specifiers, as described in Section 7.6.4.

When using the REWRITE statement (a DIGITAL Fortran extension), you can use the UNIT, FMT, ERR, and IOSTAT specifiers.

For More Information

7.6.2 Record Access Modes and File Sharing

Record access refers to how records will be read from or written to a file, regardless of its organization. Record access is specified each time you open a file; it can be different each time. The type of record access permitted is determined by the combination of file organization and record type.

For instance, you can:

7.6.2.1 Sequential Access

Sequential access transfers records sequentially to or from files or I/O devices such as terminals. You can use sequential I/O with any type of supported file organization and record type.

If you select sequential access mode for files with sequential or relative organization, records are written to or read from the file starting at the beginning of the file and continuing through it, one record after another. A particular record can be retrieved only after all of the records preceding it have been read; new records can be written only at the end of the file.

7.6.2.2 Direct Access

Direct access transfers records selected by record number to and from either sequential files stored on disk with a fixed-length record type or relative organization files.

If you select direct access mode, you can determine the order in which records are read or written. Each READ or WRITE statement must include the relative record number, indicating the record to be read or written.

You can directly access a sequential disk file only if it contains fixed-length records. Because direct access uses cell numbers to find records, you can enter successive READ or WRITE statements requesting records that either precede or follow previously requested records. For example, the first of the following statements reads record 24; the second reads record 10:


READ (12,REC=24) I 
READ (12,REC=10) J 

7.6.2.3 Limitations of Record Access by File Organization and Record Type

You can use both access modes on sequential and relative files. However, direct access to a sequential organization file can only be done if the file resides on disk and contains fixed-length records.

Table 7-8 summarizes the types of access permitted for the various combinations of file organizations and record types.

Table 7-8 Allowed Record Access for File Organizations and Record Types
Organization Record Type Sequential Access Direct Access
Sequential file Fixed
Variable
Segmented
Stream
Stream_CR
Stream_LF
Yes
Yes
Yes
Yes
Yes
Yes
Yes 1
No
No
No
No
No
Relative file 1 Fixed Yes Yes


1Direct access and relative files require that the file resides on a disk device.


Previous Next Contents Index