Previous | Contents | Index |
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).
Compaq Fortran 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) and bash shell (bash) (L*X ONLY), 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 shell or Korn shell or bash shell (L*X ONLY), use the unset command:
$ unset FORT8 |
Implied Compaq 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 95/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 95/90 file names that are associated with standard I/O files. Table 7-7 shows these relationships.
Compaq Fortran 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 Compaq Fortran 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 .
The INQUIRE statement returns information about a file and has three forms:
An inquiry by unit is usually done for an opened (connected) file. An inquiry by unit causes the Compaq Fortran 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) |
An inquiry by name causes the Compaq Fortran 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_OPEN, 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) |
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 Compaq 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.
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.
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 Compaq 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 Compaq Fortran extension), you can use the UNIT, FMT, ERR, and IOSTAT specifiers.
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:
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 |
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.
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 |
Depending on the value specified by the ACTION (or READONLY) specifier in the OPEN statement, the file will be opened by your program for reading, writing, or both reading and writing records. This simply checks that the program itself executes the type of statements intended.
For performance reasons, record-locking and shared-file checking are not supported by the current version of Compaq Fortran on Compaq Tru64 UNIX and Linux systems. When you open the file, access is always granted, regardless of whether:
Similarly, the UNLOCK statement is ignored using the current version of Compaq Fortran.
You might open a file for writing records (or reading and writing
records) and know another process might simultaneously have the file
open and be writing records. In this case, you need to coordinate
access times among those processes to handle the possibility of
simultaneous WRITE and REWRITE statements on the same record positions.
7.6.3 Specifying the Initial Record Position
When you open a disk file, you can use the OPEN statement's POSITION specifier to request one of the following initial record positions within the file:
The following I/O statements allow you to change the current record position:
Unless you use nonadvancing I/O (see Section 7.6.4), reading and
writing records usually advances the current record position by one
record. As discussed in Section 7.6.5, more than one record might be
transferred using a single record I/O statement.
7.6.4 Advancing and Nonadvancing Record I/O
After you open a file, if you omit the ADVANCE specifier (or specify ADVANCE= ' YES ' ) in READ and WRITE statements, advancing I/O (normal FORTRAN-77 I/O) will be used for record access. When using advancing I/O:
You can request nonadvancing I/O for the file by specifying the ADVANCE= ' NO ' specifier in a READ and WRITE statement. You can use nonadvancing I/O only for sequential access to external files using formatted I/O (not list-directed or namelist).
When you use nonadvancing I/O, the current record position does not change, and part of record might be transferred, unlike advancing I/O where one entire record or records are always transferred.
You can alternate between advancing and nonadvancing I/O by specifying different values for the ADVANCE specifier ( ' YES ' and ' NO ' ) in the READ and WRITE record I/O statements.
When reading records with either advancing or nonadvancing I/O, you can use the END branch specifier to branch to a specified label when the end of the file is read.
Because nonadvancing I/O might not read an entire record, it also supports an EOR branch specifier to branch to a specified label when the end of the record is read. If you omit the EOR and the IOSTAT specifiers when using nonadvancing I/O, an error results when the end-of-record is read.
When using nonadvancing input, you can use the SIZE specifier to return the number of characters read. For example, in the following READ statement, SIZE=X (where variable X is an integer) returns the number of characters read in X and an end-of-record condition causes a branch to label 700:
150 FORMAT (F10.2, F10.2, I6) READ (UNIT=20, FMT=150, SIZE=X, ADVANCE='NO', EOR=700) A, F, I |
I/O statements transfer all data as records. The amount of data that a record can contain depends on the following circumstances:
Typically, the data transferred by an I/O statement is read from or
written to a single record. It is possible, however, for a single I/O
statement to transfer data from or to more than one record, depending
on the form of I/O used.
7.6.5.1 Input Record Transfer
When using advancing I/O, if an input statement specifies fewer data fields (less data) than the record contains, the remaining fields are ignored.
If an input statement specifies more data fields than the record contains, one of the following occurs:
If an output statement specifies fewer data fields than the record contains (less data than required to fill a record), the following occurs:
If the output statement specifies more data than the record can contain, an error occurs, as follows:
Previous | Next | Contents | Index |