| Application Programmer's I/O Guide - S-3695-35 | ||
|---|---|---|
| Prev Section | Chapter 2. Standard Fortran I/O | Next Section |
A Fortran unit identifier is required for Fortran READ or WRITE statements to uniquely identify the file. A unit identifier can be one of the following:
An integer variable or expression whose value is greater than or equal to 0. Each integer unit identifier i is associated with the fort.i file, which may exist (except as noted in the following text). For example, unit 10 is associated with the fort.10 file in the current directory.
An asterisk (*) is allowed only on READ and WRITE statements. It identifies a particular file that is connected for formatted, sequential access. On READ statements, an asterisk refers to unit 100 (standard input). On WRITE statements, an asterisk refers to unit 101 (standard output).
A Hollerith (integer) variable consisting of 1 to 8 left-justified, blank-filled or zero-filled ASCII characters. Each Hollerith unit identifier is associated with the file of the same name, which may exist. For example, unit 'red'L is associated with the red file in the current working directory. The use of uppercase and lowercase characters is significant for file names. This extension is supported only on 64-bit systems.
Certain Fortran I/O statements have an implied unit number. The PRINT statement always refers to unit 101 (standard output), and the outmoded PUNCH statement always refers to unit 102 (standard error).
Fortran INQUIRE and CLOSE statements may refer to any valid or invalid unit number (if referring to an invalid unit number, no error is returned). All other Fortran I/O statements may refer only to valid unit numbers. For the purposes of an executing Fortran program, all unit numbers in use or available for use by that program are valid; that is, they exist. All unit numbers not available for use are not valid; that is, they do not exist.
Valid unit numbers are all nonnegative numbers except 100 through 102. Unit numbers 0, 5, and 6 are associated with the standard error, standard input, and standard output files; any unit can also refer to a pipe. All other valid unit numbers are associated with the fort.i file, or with the file name implied in a Hollerith unit number. Use the INQUIRE statement to check the validity (existence) of any unit number prior to using it, as in the following example:
logical UNITOK, UNITOP... inquire (unit=I,exist=UNITOK,opened=UNITOP) if (UNITOK .and. .not. UNITOP) then open (unit = I, ...) endif |
All valid units are initially closed. A unit is connected to a file as the result of one of three methods of opening a file or a unit:
An implicit open occurs when the first reference to a unit number is an I/O statement other than OPEN, CLOSE, INQUIRE, BACKSPACE, ENDFILE, or REWIND. The following example shows an implicit open:
WRITE (4) I,J,K |
If unit number 4 is not open, the WRITE statement causes it to be connected to the associated file fort.4, unless overridden by an assign command that references unit 4.
The BACKSPACE, ENDFILE, and REWIND statements do not perform an implicit OPEN. If the unit is not connected to a file, the requested operation is ignored.
An explicit unnamed open occurs when the first reference to a unit number is an OPEN statement without a FILE specifier. The following example shows an explicit unnamed open:
OPEN (7, FORM='UNFORMATTED') |
If unit number 7 is not open, the OPEN statement causes it to be connected to the associated file fort.7, unless an assign(1) command that references unit 7 overrides the default file name.
An explicit named open occurs when the first reference to a unit number is an OPEN statement with a FILE specifier. The following is an example:
OPEN (9, FILE='blue') |
If unit number 9 is not open, the OPEN statement causes it to be connected to file blue, unless overridden by an assign command that references the file named blue.
Unit numbers 100, 101, and 102 are permanently associated with the standard input, standard output, and standard error files, respectively. These files can be referenced on READ and WRITE statements. A CLOSE statement on these unit numbers has no effect. An INQUIRE statement on these unit numbers indicates they are nonexistent (not valid).
These unit numbers exist to allow guaranteed access to the standard input, standard output, and standard error files without regard to any unit actions taken by an executing program. Thus, a READ or WRITE I/O statement with an asterisk unit identifier (which is equivalent to unit 101) or a PRINT statement always works. Nonstandard I/O operations such as BUFFER IN and BUFFER OUT, READMS, and WRITMS on these units are not supported.
Fortran applications or library subroutines that must access the standard input, standard output, and standard error files can be certain of access by using unit numbers 100 through 102, even if the user program closes or reuses unit numbers 0, 5, and 6.
For all unit numbers associated with the standard input, standard output, and standard error files, the access mode and form must be sequential and formatted. The standard input file is read only, and the standard output and standard error files are write only. REWIND and BACKSPACE statements are permitted on workstation files but have no effect. ENDFILE statements are permitted on terminal files unless they are read only. The ENDFILE statement writes a logical endfile record.
The REWIND statement is not valid for any unit numbers associated with pipes. The BACKSPACE statement is not valid if the device on which the file exists does not support repositioning. BACKSPACE after a logical endfile record does not require repositioning because the endfile record is only a logical representation of an endfile record.
| Prev Section | Table of Contents | Title Page | Next Section |
| Standard Fortran I/O | Up one level | Data transfer statements |