WRITE

Purpose

The WRITE statement is a data transfer output statement.

Syntax

Read syntax diagramSkip visual syntax diagram>>-WRITE--(--io_control_list--)--+------------------+----------><
                                 '-output_item_list-'
 

output_item
is an output list item. An output list specifies the data to be transferred. An output list item can be:
  • A variable name. An array is treated as if all of its elements were specified in the order in which they are arranged in storage.

    A pointer must be associated with a target, and an allocatable object must be allocated. A derived-type object cannot have any ultimate component that is outside the scoping unit of this statement. The evaluation of output_item cannot result in a derived-type object that contains a pointer. The structure components of a structure in a formatted statement are treated as if they appear in the order of the derived-type definition; in an unformatted statement, the structure components are treated as a single value in their internal representation (including padding).

  • An expression
  • An implied-DO list, as described under Implied-DO List
Fortran 2003 Standard

An output_item must not be a procedure pointer.

End of Fortran 2003 Standard
io_control
is a list that must contain one unit specifier (UNIT=), and can also contain one of each of the other valid specifiers:
[UNIT=] u
is a unit specifier that specifies the unit to be used in the output operation. u is an external unit identifier or internal file identifier.
IBM Extension

An external unit identifier refers to an external file. It is one of the following:

  • An integer expression whose value is in the range 0 through 2,147,483,647.
  • An asterisk, which identifies external unit 6 and is preconnected to standard output.
End of IBM Extension

An internal file identifier refers to an internal file. It is the name of a character variable, which cannot be an array section with a vector subscript.

If the optional characters UNIT= are omitted, u must be the first item in io_control_list. If UNIT= is specified, FMT= must also be specified.

[FMT=] format
is a format specifier that specifies the format to be used in the output operation. format is a format identifier that can be:
  • The statement label of a FORMAT statement. The FORMAT statement must be in the same scoping unit.
  • The name of a scalar INTEGER(4) or INTEGER(8) variable that was assigned the statement label of a FORMAT statement. The FORMAT statement must be in the same scoping unit.
    Fortran 95

    Fortran 95 does not permit assigning of a statement label.

    End of Fortran 95
  • A character constant enclosed in parentheses. Only the format codes listed under FORMAT can be used between the parentheses. Blank characters can precede the left parenthesis or follow the right parenthesis.
  • A character variable that contains character data whose leftmost character positions constitute a valid format. A valid format begins with a left parenthesis and ends with a right parenthesis. Only the format codes described in the FORMAT statement can be used between the parentheses. Blank characters can precede the left parenthesis or follow the right parenthesis. If format is an array element, the format identifier must not exceed the length of the array element.
  • An array of noncharacter intrinsic type. The data must be a valid format identifier as described under character array.
  • Any character expression, except one involving concatenation of an operand that specifies inherited length, unless the operand is the name of a constant.
  • An asterisk, specifying list-directed formatting.
  • A namelist specifier that specifies the name of a namelist list that you have previously defined.

If the optional characters FMT= are omitted, format must be the second item in io_control_list, and the first item must be the unit specifier with UNIT= omitted. NML= and FMT= cannot both be specified in the same output statement.

POS=integer_expr
Fortran 2003 Begins integer_expr is an integer expression greater than 0. POS= specifies the file position of the file storage unit to be written in a file connected for stream access. You must not use POS= for a file that cannot be positioned. Fortran 2003 Ends
REC= integer_expr
is a record specifier that specifies the number of the record to be written in a file connected for direct access. The REC= specifier is only permitted for direct output. integer_expr is an integer expression whose value is positive. A record specifier is not valid if formatting is list-directed or if the unit specifier specifies an internal file. The record specifier represents the relative position of a record within a file. The relative position number of the first record is 1. You must not specify REC= in data transfer statements that specify a unit connected for stream access, or use the POS= specifier.
Fortran 2003 Standard
IOMSG= iomsg_variable
is an input/output status specifier that specifies the message returned by the input/output operation. iomsg_variable is a scalar default character variable. It must not be a use-associated nonpointer protected variable. When the input/output statement containing this specifier finishes execution, iomsg_variable is defined as follows:
  • If an error, end-of-file, or end-of-record condition occurs, the variable is assigned an explanatory message as if by assignment.
  • If no such condition occurs, the value of the variable is unchanged.
End of Fortran 2003 Standard
IOSTAT= ios
is an input/output status specifier that specifies the status of the input/output operation. ios is an integer variable. Coding the IOSTAT= specifier suppresses error messages. When the statement finishes execution, ios is defined with:
  • A zero value if no error condition occurs
  • A positive value if an error occurs.
IBM Extension
ID= integer_variable
indicates that the data transfer is to be done asynchronously. The integer_variable is an integer variable. If no error is encountered, the integer_variable is defined with a value after executing the asynchronous data transfer statement. This value must be used in the matching WAIT statement.

Asynchronous data transfer must either be direct unformatted, sequential unformatted, or stream unformatted. Asynchronous I/O to internal files is prohibited. Asynchronous I/O to raw character devices (for example, tapes or raw logical volumes) is prohibited. The integer_variable must not be associated with any entity in the data transfer I/O list, or with a do_variable of an io_implied_do in the data transfer I/O list. If the integer_variable is an array element reference, its subscript values must not be affected by the data transfer, the io_implied_do processing, or the definition or evaluation of any other specifier in the io_control_spec.

End of IBM Extension
Fortran 2003 Standard
DELIM= char_expr
specifies what delimiter, if any, is used to delimit character constants written with list-directed or namelist formatting. char_expr is a scalar character expression whose value must evaluate to APOSTROPHE, QUOTE, or NONE. If the value is APOSTROPHE, apostrophes delimit character constants and all apostrophes within character constants are doubled. If the value is QUOTE, double quotation marks delimit character constants and all double quotation marks within character constants are doubled. If the value is NONE, character constants are not delimited and no characters are doubled. The default value is NONE. The DELIM= specifier is permitted only for files being connected for formatted input/output, although it is ignored during input of a formatted record.
End of Fortran 2003 Standard
ERR= stmt_label
is an error specifier that specifies the statement label of an executable statement in the same scoping unit to which control is to transfer in the case of an error. Coding the ERR= specifier suppresses error messages.
IBM Extension
NUM= integer_variable
is a number specifier that specifies the number of bytes of data transmitted between the I/O list and the file. integer_variable is an integer variable. The NUM= specifier is only permitted for unformatted output. Coding the NUM parameter suppresses the indication of an error that would occur if the number of bytes represented by the output list is greater than the number of bytes that can be written into the record. In this case, integer_variable is set to a value that is the maximum length record that can be written. Data from remaining output list items is not written into subsequent records. In the portion of the program that executes between the asynchronous data transfer statement and the matching WAIT statement, the integer_variable in the NUM= specifier or any variable associated with it must not be referenced, become defined, or become undefined.
End of IBM Extension
[NML=] name
is a namelist specifier that specifies the name of a namelist list that you have previously defined. If the optional characters NML= are not specified, the namelist name must appear as the second parameter in the list, and the first item must be the unit specifier with UNIT= omitted. If both NML= and UNIT= are specified, all the parameters can appear in any order. The NML= specifier is an alternative to FMT=. Both NML= and FMT= cannot be specified in the same output statement.
ADVANCE= char_expr
is an advance specifier that determines whether nonadvancing output occurs for this statement. char_expr is a character expression that must evaluate to YES or NO. If NO is specified, nonadvancing output occurs. If YES is specified, advancing, formatted sequential or formatted stream output occurs. The default value is YES. ADVANCE= can be specified only in a formatted sequential WRITE statement with an explicit format specification that does not specify an internal file unit specifier.

Implied-DO List

Read syntax diagramSkip visual syntax diagram>>-(--do_object_list-- , --------------------------------------->
 
>--do_variable = arith_expr1arith_expr2----------------------->
 
>--+---+--+-------------+--)-----------------------------------><
   '-,-'  '-arith_expr3-'
 

do_object
is an output list item
do_variable
is a named scalar variable of type integer or real
arith_expr1, arith_expr2, and arith_expr3
are scalar numeric expressions

The range of an implied-DO list is the list do_object_list. The iteration count and values of the DO variable are established from arith_expr1, arith_expr2, and arith_expr3, the same as for a DO statement. When the implied-DO list is executed, the items in the do_object_list are specified once for each iteration of the implied-DO list, with the appropriate substitution of values for any occurrence of the DO variable.

Rules

IBM Extension

If a NUM= specifier is present, neither a format specifier nor a namelist specifier can be present.

End of IBM Extension

Variables specified for the IOSTAT= and NUM= specifiers must not be associated with any output list item, namelist list item, or DO variable of an implied-DO list. If such a specifier variable is an array element, its subscript values must not be affected by the data transfer, any implied-DO processing, or the definition or evaluation of any other specifier.

If the ERR= and IOSTAT= specifiers are set and an error is encountered during a synchronous data transfer, transfer is made to the statement specified by the ERR= specifier and a positive integer value is assigned to ios.

IBM Extension

If the ERR= or IOSTAT= specifiers are set and an error is encountered during an asynchronous data transfer, execution of the matching WAIT statement is not required.

If a conversion error is encountered and the CNVERR run-time option is set to NO, ERR= is not branched to, although IOSTAT= may be set.

If IOSTAT= and ERR= are not specified,

End of IBM Extension

PRINT format has the same effect as WRITE(*,format).

Examples

WRITE (6,FMT='(10F8.2)') (LOG(A(I)),I=1,N+9,K),G

Related information