Outputs data to external
and internal files.
Syntax
WRITE (io-specifier-list) [output-list] |
 |
- output-list
is a list of comma-separated data items for output.
The data items can include expressions and implied-DO.
- io-specifier-list
is a list of the following comma-separated I/O specifiers:
- [UNIT=]unit
specifies the unit connected
to the output file. unit can be one of the following:
The name of a character variable,
indicating an internal file
An integer expression that evaluates to the unit
connected to an external file
An asterisk, indicating the preconnected unit 6
(standard output)
If the optional keyword UNIT= is omitted, unit must be the first item in io-specifier-list. This is the only specifier required in io-specifier-list.
- [FMT=] format
specifies the
format specification for formatting the data. format can be one of the following:
An asterisk (*), specifying list-directed I/O
The label of a FORMAT statement containing the format specification
An integer variable that has been assigned the label
of a FORMAT statement
An embedded format
specification
If the optional keyword FMT= is omitted, format must be the second item in io-specifier-list.
 |
 |  |
 |
 | NOTE: The NML= and FMT= specifier may not both appear in the same io-specifier-list. |
 |
 |  |
 |
- [NML=]name
specifies the
name of a namelist group for namelist-directed output. name must have been defined in a NAMELIST statement. If the optional keyword NML= is omitted, name must be the second item in the list. The first item must
be the unit specifier without the optional keyword UNIT=.
The NML= and FMT= specifier may not both appear in the same io-specifier-list.
- ADVANCE=character-expression
specifies whether
to use advancing I/O for this statement. character-expression can be one of the following arguments:
Table 10-54 Title not available (WRITE)
| 'YES' | Use
advancing formatted sequential I/O default. |
| 'NO' | Use nonadvancing formatted sequential I/O. |
If the ADVANCE= specifier appears in io-specifier-list, unit must be connected to an external file opened for
formatted sequential I/O. Nonadvancing I/O is incompatible with
list-directed and namelist I/O.
- ERR=stmt-label
specifies the
label of the executable statement to which control passes if an error
occurs during statement execution.
- IOSTAT=integer-variable
returns the I/O
status after the statement executes. If the statement executes successfully, integer-variable is set to zero. If an error occurs, it is set to
a positive integer that indicates which error occurred.
- REC=integer-expression
specifies the
number of the record to be written to the file connected for direct
access. This specifier cannot appear in io-specifier-list with the NML= and ADVANCE= specifiers, nor with FMT=* (for list-directed I/O).
Description
The WRITE statement transfers data from internal storage
to an external or internal file. An external file can be opened
for sequential access or direct access I/O. If it is opened for sequential
access, the WRITE statement can perform the following types of I/O:
If the file is opened for direct access, the WRITE statement can perform formatted or unformatted
I/O.
WRITE statements operating on internal files can perform
formatted or list-directed I/O.
For detailed information about files and different types of
I/O, see Chapter 8 “I/O
and file handling”.
Examples
The examples in this section illustrate different uses of
the WRITE statement.
Nonadvancing I/O
CHARACTER(LEN=17) :: prompt = 'Enter a number: ' WRITE (6, '(A)', ADVANCE='NO') prompt |
The WRITE statement outputs to the file connected to unit
6, which is preconnected to standard output. The ADVANCE='NO' specifier indicates the following:
The file has been opened for formatted
sequential I/O.
The statement uses nonadvancing I/O to read an integer
formatted as four characters into the variable prompt.
The effect of the nonadvancing WRITE is to output the character string in prompt to standard output without a terminating newline.
This means that anything subsequently entered by the user will appear
on the same line.
Internal file
CHARACTER(LEN=80) :: cfile WRITE (cfile, '(I5, F10.5)') i, x |
The statement
writes a string of characters into the internal file cfile, using the embedded format specification to perform
the format conversion.
Namelist-directed I/O
In the next example,
each of the four WRITE statements following the NAMELIST statement uses a different style of syntax to
do exactly the same thing:
NAMELIST /nl/ a, b, c WRITE (UNIT=6, NML=nl) ! 6 = standard output WRITE (6, nl) WRITE (*, NML=nl) ! * = standard output WRITE nl ! assume standard output |
List-directed I/O
This statement
converts the value of int_var to character format and outputs the character string
to standard output. The format conversion is based on the type of int_var. If you knew the format, you could substitute
for the asterisk one of the following:
The label of the FORMAT statement with the format specification, as in:
WRITE (6, 100) int_var 100 FORMAT(I4) |
An embedded format specification itself, as in:
WRITE (6, '(I4)') int_var |
Unformatted direct-access I/O
WRITE (31, REC=rec_num, ERR=99, IOSTAT=ios) a, b |
This statement
outputs to the file connected to unit 31. The REC= specifier indicates that the file has been opened
for direct access and that this statement will output to the record
whose number is stored in the variable rec_num. If an I/O error occurs during the execution of
the statement, an error number will be stored in ios, and execution control will branch to the executable
statement at label 99.
Related statements
CLOSE, OPEN, PRINT, and READ
Related concepts
For information about I/O concepts, see Chapter 8 “I/O
and file handling”, which also lists example programs that
use I/O. For information about I/O formatting, see Chapter 9 “I/O
formatting”.