FORTRAN 77 Language Reference


The WRITE statement writes data from the list to a file.

Note -

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])




Unit identifier of the unit connected to the file  


Format identifier  


I/O status specifier  


Record number  


Error specifier (statement label)  


List of variables  


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.


Unit Identifier

u is either an external unit identifier or an internal file identifier.

An external unit identifier must be one of the following:

If the optional characters UNIT= are omitted from the unit specifier, then u must be the first item in the list of specifiers.

Format Identifier

f is a format identifier and can be:

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.

I/O Status Specifier

ios must be an integer variable, integer array element, or integer record field.

Record Number

rn must be a positive integer expression. This argument can appear only for direct-access files. rn can be specified for internal files. @

Error Specifier

s must be the label of an executable statement in the same program unit in which this WRITE statement occurs.

Output List

iolist can be empty, or it can contain output items or implied DO lists. The output items must be one of the following:

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.

Namelist-Directed WRITE

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:

  1. The file associated with the specified unit is determined.

    The format, if specified, is established. The file is positioned appropriately prior to data transfer.

  2. 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.

  3. 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.

  4. The file is repositioned appropriately after the data transfer.

  5. If ios is specified, and no error occurs, it is set to zero; otherwise, it is set to a positive value.

  6. If s is specified and an error occurs, control is transferred to s.


Note these restrictions:


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:


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 
8     WRITE( *, * ) 'I/O error # ', N, ', on 1' 

Example 2: Direct, unformatted write, trap I/O errors, and I/O status:

       WRITE( 1, REC=3, IOSTAT=N, ERR=8 ) V 
8     WRITE( *, * ) 'I/O error # ', N, ', on 1' 

Example 3: Direct, alternate syntax (equivalent to above example):

       WRITE( 1 ' 3, IOSTAT=N, ERR=8 ) V 
8      WRITE( *, * ) 'I/O error # ', N, ', on 1' 


Example 4: List-directed write to screen:

       WRITE( *, * ) A, V 
       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:.

       LOGICAL NEW*4 
       REAL DELTA*4 
       WRITE( 1, G ) 
       WRITE( UNIT=1, NML=G ) 
       WRITE( 1, NML=G )