Every file, internal or external, is associated with a logical device, sometimes referred to as a logical unit. You identify the logical device associated with a file by a unit specifier (UNIT=). The unit specifier for an internal file is the name of the character variable associated with it. The unit specifier for an external file is one of the following:
a number you assign with the OPEN statement
a number preconnected as a unit specifier to a device
an asterisk (*)
The OPEN statement connects a unit number with an external file and allows you to explicitly specify file attributes and run-time options using OPEN statement specifiers. External unit specifiers that are preconnected to certain devices do not have to be opened. External units that you connect are disconnected when program execution terminates or when the unit is closed by a CLOSE statement.
A unit must not be connected to more than one file at a time, and a file must not be connected to more than one unit at a time. You can OPEN an already opened file but only to change some of the I/O options for the connection, not to connect an already opened file or unit to a different unit or file.
You must use a unit specifier for all I/O statements, except in the following six cases:
A unit specifier associated with an external
file must be either an integer expression or an asterisk (*). The integer
expression must be in the range 0 (zero) to a maximum value of 2,147,483,640.
The following example connects the external file UNDAMP.DAT
to unit 10 and writes to it:
OPEN (UNIT = 10, FILE = 'undamp.dat') WRITE (10, '(A18,\)') ' Undamped Motion:'
The asterisk (*) unit specifier specifies the keyboard when reading and the screen when writing. The following example uses the asterisk specifier to write to the screen:
WRITE (*, '(1X, A30,\)') ' Write this to the screen.'
Intel Fortran has four units preconnected to external files (devices), as shown in the following table.
External Unit Specifier | Description |
---|---|
Asterisk (*) | Always represents the keyboard and screen (unless the appropriate environment variable is defined, such as FOR_READ). |
0 | Initially represents the screen (unless FORT0 is defined) |
5 | Initially represents the keyboard (unless FORT5 is defined) |
6 | Initially represents the screen (unless FORT6 is defined) |
The asterisk (*) specifier is the only unit specifier that cannot be reconnected to another file, and attempting to close this unit causes a compile-time error. Units 0, 5, and 6, however, can be connected to any file with the OPEN statement. If you close unit 0, 5, or 6, it is automatically reconnected to its preconnected device the next time an I/O statement attempts to use that unit.
When you omit the file name in the OPEN statement or use an implicit OPEN, you can define the environment variable FORTn to specify the file name for a particular unit number n (except when the compiler option -fpscomp filesfromcmd is not specified). For example, if you want unit 6 to write to a file instead of standard output, set the environment variable FORT6 to the path and filename to be used before you run the program.
The following example writes to the preconnected unit 6 (the screen), then reconnects unit 6 to an external file and writes to it, and finally reconnects unit 6 to the screen and writes to it:
REAL a, b ! Write to the screen (preconnected unit 6). WRITE(6, '('' This is unit 6'')') ! Use the OPEN statement to connect unit 6 ! to an external file named 'COSINES'. OPEN (UNIT = 6, FILE = 'COSINES', STATUS = 'NEW') DO a = 0.1, 6.3, 0.1 b = COS (a) ! Write to the file 'COSINES'. WRITE (6, 100) a, b 100 FORMAT (F3.1, F5.2) END DO ! Close it. CLOSE (6) ! Reconnect unit 6 to the screen, by writing to it. WRITE(6,' ('' Cosines completed'')') END
Note
The association between the logical unit number and the physical file can occur at run-time. Instead of changing the logical unit numbers specified in the source program, you can change this association at run time 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.
The unit specifier associated with an internal file is a character string or character array. There are two types of internal files:
Follow these rules when using internal files:
You can read and write internal files with FORMAT I/O statements or list-directed I/O statements exactly as you can external files. Before an I/O statement is executed, internal files are positioned at the beginning, before the first record.
With internal files, you can use the formatting capabilities of the I/O system to convert values between external character representations and Fortran internal memory representations. That is, reading from an internal file converts the ASCII representations into numeric, logical, or character representations, and writing to an internal file converts these representations into their ASCII representations.
This feature makes it possible to read a string of characters without knowing its exact format, examine the string, and interpret its contents. It also makes it possible, as in dialog boxes, for the user to enter a string and for your application to interpret it as a number.
If less than an entire record is written to an internal file, the rest of the record is filled with blanks.
In the following example, str
and
fname
specify internal files:
CHARACTER(10) str INTEGER n1, n2, n3 CHARACTER(14) fname INTEGER i str = " 1 2 3" ! List-directed READ sets n1 = 1, n2 = 2, n3 = 3. READ(str, *) n1, n2, n3 i = 4 ! Formatted WRITE sets fname = 'FM004.DAT'. WRITE (fname, 200) i 200 FORMAT ('FM', I3.3, '.DAT')