The WRITE statement writes data from the list to a file.
For tape I/O, use the TOPEN() routines.
WRITE([UNIT=] u [, [FMT=] f] [, IOSTAT=ios] [, REC=rn] [, ERR=s]) iolist
WRITE([ UNIT=] u, [NML=] grname [, IOSTAT=ios] [, ERR=s])
Parameter |
Description |
u |
Unit identifier of the unit connected to the file |
f |
Format identifier |
ios |
I/O status specifier |
rn |
Record number |
s |
Error specifier (statement label) |
iolist |
List of variables |
grname |
Name of the namelist group |
The options can be specified in any order.
An alternate for the REC=rn form is allowed, as follows: @
WRITE( u ' rn ) iolist @
See Example 3, later on in this section.
u is either an external unit identifier or an internal file identifier.
An external unit identifier must be one of the following:
A nonnegative integer expression
An asterisk, identifying stdout, which is normally connected to the console
If the optional characters UNIT= are omitted from the unit specifier, then u must be the first item in the list of specifiers.
f is a format identifier and can be:
An asterisk (*), indicating list-directed I/O. See "List-Directed I/O " for more information.
The label of a FORMAT statement that appears in the same program unit
An integer variable name that has been assigned the label of a FORMAT statement that appears in the same program unit
A character expression or integer array that specifies the format string. This is called a runtime format or a variable format. The integer array is nonstandard. @
See "Runtime Formats " for details on formats evaluated at runtime.
If the optional characters, FMT=, are omitted from the format specifier, then f must appear as the second argument for a formatted write; otherwise, it must not appear at all.
f must not be an asterisk for direct access.
f can be an asterisk for internal files. @
If a file is connected for formatted I/O, unformatted data transfer is prohibited, and vice versa.
ios must be an integer variable, integer array element, or integer record field.
rn must be a positive integer expression. This argument can appear only for direct-access files. rn can be specified for internal files. @
s must be the label of an executable statement in the same program unit in which this WRITE statement occurs.
iolist can be empty, or it can contain output items or implied DO lists. The output items must be one of the following:
Variables
Substrings
Arrays
Array elements
Record fields
Any other expression
A simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.
Implied DO lists are described in "Implied DO Lists".
If the output item is a character expression that employs the concatenation operator, the length specifiers of its operands can be an asterisk (*). This rule is nonstandard. @
If a function appears in the output list, that function must not cause an input/output statement to be executed.
The second form of WRITE is used to output the items of the specified namelist group. Here, grname is the name of the list previously defined in a NAMELIST statement.
Execution proceeds as follows:
The file associated with the specified unit is determined.
The format, if specified, is established. The file is positioned appropriately prior to data transfer.
If the output list is not empty, data is transferred from the list to the file.
Data is edited according to the format, if specified.
In the second form of namelist-directed WRITE, the data is transferred from the items of the specified namelist group according to the rules of namelist-directed output.
The file is repositioned appropriately after the data transfer.
If ios is specified, and no error occurs, it is set to zero; otherwise, it is set to a positive value.
If s is specified and an error occurs, control is transferred to s.
Note these restrictions:
Output from an exception handler is unpredictable.
If you make your own exception handler, do not do any FORTRAN output from it. If you must do some, then call abort right after the output. Doing so reduces the relative risk of a system freeze. FORTRAN I/O from an exception handler amounts to recursive I/O. See the next paragraph.
Recursive I/O does not work reliably.
If you list a function in an I/O list, and if that function does I/O, then during runtime, the execution may freeze, or some other unpredictable problem results. This risk exists independent of using parallelization.
Example: Recursive I/O fails intermittently:
WRITE(*,*) x, f(x) Not allowed because f() does I/O. END FUNCTION F(X) WRITE(*,*) X RETURN END
If u specifies an external unit that is not connected to a file, an implicit OPEN operation is performed that is equivalent to opening the file with the following options:
OPEN(u , FILE='FORT.u ', STATUS='UNKNOWN', & ACCESS='SEQUENTIAL', FORM=fmt )
The value of fmt is 'FORMATTED' if the write is formatted, and 'UNFORMATTED' otherwise.
A simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.
The record number for direct-access files starts from one onwards.
Namelist-directed output is permitted on sequential access files only.
Example 1: Formatted write with trap I/O errors and I/O status:
WRITE( 1, 2, ERR=8, IOSTAT=N ) X, Y RETURN ... 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' STOP END
Example 2: Direct, unformatted write, trap I/O errors, and I/O status:
... WRITE( 1, REC=3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END
Example 3: Direct, alternate syntax (equivalent to above example):
... WRITE( 1 ' 3, IOSTAT=N, ERR=8 ) V @ ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END
Example 4: List-directed write to screen:
WRITE( *, * ) A, V or PRINT *, A, V
Example 5: Formatted write to an internal file:
CHARACTER CA*16, L*8 /'abcdefgh'/, R*8 /'ijklmnop'/ WRITE( CA, 1 ) L, R 1 FORMAT( 2 A8 )
Example 6: Write an entire array
DIMENSION V(5) WRITE( 3, '(5F4.1)') V
Example 7: Namelist-directed write:.
CHARACTER SAMPLE*16 LOGICAL NEW*4 REAL DELTA*4 NAMELIST /G/ SAMPLE, NEW, DELTA ... WRITE( 1, G ) or WRITE( UNIT=1, NML=G ) or WRITE( 1, NML=G )