Every file, internal or external, is associated with a logical device. 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 assigned by the Intel® Fortran run-time library with the OPEN specifier NEWUNIT=
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:
ACCEPT, which always reads from standard input, unless the FOR_ACCEPT environment variable is defined.
INQUIRE by file, which specifies the filename, rather than the unit with which the file is associated.
PRINT, which always writes to standard output, unless the FOR_PRINT environment variable is defined.
READ statements that contain only an I/O list and format specifier, which read from standard input (UNIT=5), unless the FOR_READ environment variable is defined.
WRITE statements that contain only an I/O list and format specifier, which write to standard output, unless the FOR_PRINT environment variable is defined.
TYPE, which always writes to standard output, unless the FOR_TYPE environment variable is defined.
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 predefined parameters FOR_K_PRINT_UNITNO, FOR_K_TYPE_UNITNO, FOR_K_ACCEPT_UNITNO, and FOR_K_READ_UNITNO may not be in that range. For more information, see the Language Reference.)
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 |
Environment Variable |
Description |
Asterisk (*) |
None |
Always represents the keyboard and screen (unless the appropriate environment variable is defined, such as FOR_READ). |
0 |
FORT0 |
Initially represents the screen (unless FORT0 is explicitly defined) |
5 |
FORT5 |
Initially represents the keyboard (unless FORT5 is explicitly defined) |
6 |
FORT6 |
Initially represents the screen (unless FORT6 is explicitly 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.
Intel® Fortran does not support buffering to stdout or stdin. All I/O to units * and 6 use line buffering. Therefore, C and Fortran output to stdout should work well as long as the C code is not performing buffering. In the case of buffering, the C code will have to flush the buffers with each write. For more information on stdout and stdin, see Assigning Files to Logical Units.
You can change these preconnected files by doing one of the following:
Using an OPEN statement to open unit 5, 6, or 0. When you explicitly OPEN a file for unit 5, 6, or 0, the OPEN statement keywords specify the file-related information to be used instead of the preconnected standard I/O file.
Setting the appropriate environment variable (FORTn) to redirect I/O to an external file.
To redirect input or output from the standard preconnected files at run time, you can set the appropriate environment variable or use the appropriate shell redirection character in a pipe (such as > or <).
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. An exception to this is when the compiler option -fpscomp filesfromcmd (Linux* OS and Mac OS* X) or /fpscomp:filesfromcmd (Windows) is 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. If the appropriate environment variable is not defined, a default filename is used, in the form fort.n where n is the logical unit number.
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
The unit specifier associated with an internal file is a character string or character array. There are two types of internal files:
An internal file that is a character variable, character array element, or noncharacter array element has exactly one record, which is the same length as the variable, array element, or noncharacter array element.
An internal file that is a character array, a character derived type, or a noncharacter array is a sequence of elements, each of which is a record. The order of records is the same as the order of array elements or type elements, and the record length is the length of one array element or the length of the derived-type element.
Follow these rules when using internal files:
Use only formatted I/O, including I/O formatted with a format specification and list-directed I/O. (List-directed I/O is treated as sequential formatted I/O.) Namelist formatting is not allowed.
If the character variable is an allocatable array or array part of an allocatable array, the array must be allocated before use as an internal file. If the character variable is a pointer, it must be associated with a target.
Use only READ and WRITE statements. You cannot use file connection (OPEN, CLOSE), file positioning (REWIND, BACKSPACE), or file inquiry (INQUIRE) statements with 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 of the variable, 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')
Copyright © 1996-2010, Intel Corporation. All rights reserved.