WRITE statement

Standard: (F77) F90 F95 F2003 F2008 Example program


<Syntax>

WRITE ( ci-spec [ , ci-spec ]... ) [ output-list ]

output-list ::= output-item [ , output-item ]...
output-item ::= expr | ( output-list , do-spec )

do-spec ::= variable-name = m1 , m2 [ , m3 ]
m1 ::= initial-scalar-integer-expr
m2 ::= final-scalar-integer-expr
m3 ::= step-scalar-integer-expr

The possible ci-specs are listed in the table below, together with their meaning.

The UNIT= ci-spec is required; the UNIT= keyword and equals sign may be omitted if it is the first ci-spec. If UNIT= was omitted, the FMT= keyword and equals sign may omitted if it is the second ci-spec, or the NML= keyword and equals sign may be omitted if it is the second ci-spec.

<Semantics>

The WRITE statement writes one or more records, or partial records, to the specified unit. Each ci-spec is listed in the table below.

If unit is *, the output is to the standard output unit (usually the screen); if it is a character variable, the output is to that variable; if it is an integer expr, the output is to the external unit with that number, which must be non-negative.

If format is present, the WRITE is a formatted WRITE; if format is *, list-directed formatting is used; if format is a label, that FORMAT statement is used; if format is a character expression, that expression is interpreted as an explicit format. The WRITE is also a formatted WRITE if Namelist is present, in which case namelist formatting is used.

The form “( output-list , do-spec )” evaluates the output-list for each value of variable-name implied by the do-spec; for further details see the DO statement. Implied DO loops may be nested.

Specifier Argument Meaning Values
[ UNIT = ] unit Unit to write to
[ FMT = ] format Formatting
ADVANCE= Character Advancing input? 'YES', 'NO'
ASYNCHRONOUS= Character Async i/o? 'YES', 'NO'
DECIMAL= Character Decimal edit mode 'COMMA', 'POINT'
DELIM= Character Delimiter mode 'NONE', 'QUOTE', 'APOSTROPHE'
ERR= Label Branch on error
ID= Integer Pending i/o identifier
IOMSG= Character Error message
IOSTAT= Integer I/O status
[ NML= ] Namelist Namelist formatting
POS= Integer File storage position Positive
REC= Integer Record number Positive
ROUND= Character Rounding mode 'COMPATIBLE', 'DOWN', 'NEAREST', 'PROCESSOR_DEFINED', 'UP', 'ZERO'
SIGN= Character Sign edit mode 'PLUS', 'SUPPRESS', 'PROCESSOR_DEFINED'

The ADVANCE= specifier is only allowed for external files. If the [ NML= ] specifier appears, no format is allowed. The REC= specifier is only allowed for a direct access file.

In Fortran 2003, the DECIMAL=, DELIM=, ROUND= and SIGN= specifiers control the initial edit modes for that WRITE; if omitted, the default is taken from the OPEN. The IOMSG= specifier sets its variable to an error message if an error occurs. The POS= specifier is only allowed for stream files, and specifies the file storage position at which to start writing. The ID= specifier is only allowed in conjunction with ASYNCHRONOUS='YES', and sets the variable to an identifier for that asynchronous input/output (this variable can then be used in an ID= specifier in INQUIRE or WAIT).

<Related>

ADVANCE= specifier, ASYNCHRONOUS= specifier, BACKSPACE statement, CLOSE statement, DECIMAL= specifier, DO statement, ENDFILE statement, ERR= specifier, FORMAT statement, ID= specifier, INQUIRE statement, IOMSG= specifier, IOSTAT= specifier, NAMELIST statement, NML= specifier, OPEN statement, PRINT statement, READ statement, REWIND statement, ROUND= specifier, SIGN= specifier