The WRITE statement releases a logical record for an output or
input-output file. For sequential files it can also be used for vertical
positioning of lines within a logical page.
All Formats (All Files)
- The results of the execution of the WRITE statement with the FROM
phrase is equivalent to the execution of:
- The statement:
MOVE identifier-1 TO record-name
according to the rules specified for the MOVE statement:
- The same WRITE statement without the FROM phrase.
The contents of the record area prior to the execution of the implicit
MOVE statement have no effect on the execution of this WRITE statement.
After execution of the WRITE statement is complete, the information in
the area referenced by identifier-1 is available, even though the information
in the area referenced by record-name is not. (See General Rule 13.)
- The file position indicator is unaffected by the execution of a WRITE
statement.
-
The execution of the WRITE statement causes the value of the
FILE STATUS data item, if any, associated with the file to be updated. (See the
topic
I-O Status
in the chapter Procedure Division.)
- The maximum record size for a file is established at the time the file
is created and must not subsequently be changed.
- The number of character positions on a mass storage device to store a
logical record in a file may or may not be equal to the number of character
positions defined by the logical description of that record
.
- The execution of the WRITE statement releases a logical record to the
operating system.
If the execution of the WRITE statement
is unsuccessful, the I/O status of the file-name associated with record-name is
updated and control is transferred according to the rules of the USE statement
following the execution of any USE AFTER EXCEPTION procedure applicable to the
file-name associated with record-name. (See the topic
The USE
Statement.) Format 1 (Record
Sequential Files)
When the mnemonic-name associated with TAB
is specified the result is to cause the paper to throw to the standard vertical
tabulation position.
- If the logical end of the representation of the printed page is reached
during the execution of a WRITE statement with the END-OF-PAGE phrase, the
imperative-statement specified in the END-OF-PAGE phrase is executed. The
logical end is specified in the LINAGE clause associated with record-name.
- An end-of-page condition is reached whenever the execution of a given
WRITE statement with the END-OF-PAGE phrase occurs, when the execution of such
a WRITE statement causes the LINAGE-COUNTER to equal or exceed the value
specified by integer-2 or the data item referenced by data-name-2 of the LINAGE
clause, if specified. In this case, the WRITE statement is executed and then
the imperative statement in the END-OF-PAGE phrase is executed.
An automatic page overflow condition is reached whenever the execution
of a given WRITE statement (with or without an END-OF-PAGE phrase) cannot be
fully accommodated within the current page body.
This occurs when a WRITE statement, if executed, would cause the
LINAGE-COUNTER to exceed the value specified by integer-1 or the data item
referenced by data-name-1 of the LINAGE clause. In this case, the record is
presented on the logical page before or after (depending on the phrase used)
the device is repositioned to the first line that can be written on the next
logical page as specified in the LINAGE clause. The imperative statement in the
END-OF-PAGE clause, if specified, is executed after the record is written and
the device has been repositioned.
If integer-2 or data-name-2 of the LINAGE clause is not specified, no
end-of-page condition distinct from the page overflow condition is detected. In
this case, the end-of-page condition and page overflow condition occur
simultaneously.
If integer-2 or data-name-2 of the LINAGE clause is specified, but the
execution of a given WRITE statement would cause LINAGE-COUNTER to
simultaneously exceed the value of both integer-2 or the data item referenced
by data-name-2 and integer-1 or the data item referenced by data-name-1, then
the operation proceeds as if integer-2 or data-name-2 had not been specified.
Format 1
(Line Sequential Files)
If the ADVANCING phrase is not used,
automatic advancing of one line is provided to act in accordance with the
convention of your operating system text editor (usually as if you had
specified BEFORE ADVANCING 1 LINE).

When an
attempt is made to write beyond the externally defined boundaries of a
sequential file, an exception condition exists and the contents of the record
area are unaffected. The following actions take place:
- The value of the FILE STATUS data item, if any, of the associated
file is set to a value indicating a boundary violation. (See the topic
I-O
Status in the chapter Procedure
Division.)
- If a USE AFTER STANDARD EXCEPTION declarative is explicitly or
implicitly specified for the file, that declarative procedure is executed.
- If a USE AFTER STANDARD EXCEPTION declarative is not explicitly or
implicitly specified for the file, the result is undefined.

After the
recognition of an end-of-unit of an output file that is contained on more than
one physical reel/unit, the WRITE statement performs the following operations:
- The standard ending reel/unit procedure.
- The reel/unit swap.
- The standard beginning of reel/unit label procedure.
When the file contains fixed-length
records, the entire record area is written to the file. If the record area is
redefined with records of varying sizes, records that are shorter than the
maximum size may need to be spaced-filled so that they do not contain
meaningless data.
If, during the successful execution of
a WRITE statement with the NOT END-OF-PAGE phrase, the end-of-page condition
does not occur, control is transferred to imperative-statement-2 after
execution of the input-ouput operation. Format 1 (Record
and Line Sequential Files)
- Both the ADVANCING phrase and the END-OF-PAGE phrase allow control of
the vertical positioning of each line on a representation of a printed page.
If the ADVANCING phrase is not used, automatic advancing is provided
when output is directed to a list device (PRINTER or PRINTER-1), to act as if
you had specified AFTER ADVANCING 1 LINE. If the ADVANCING phrase is used,
advancing is provided as follows:
- If identifier-2 is specified, the representation of the printed
page is advanced the number of lines equal to the current value associated with
identifier-2.
- If integer is specified, the representation of the printed page is
advanced the number of lines equal to the value of integer.
- If mnemonic-name is specified, the representation of the printed
page is advanced as specified in the topic
The Special-Names
Paragraph.
- If the BEFORE phrase is used, the line is presented before the
representation of the printed page is advanced according to rules a, b, and c
above.
- If the AFTER phrase is used, the line is presented after the
representation of the printed page is advanced according to rules a, b, and c
above.
- If PAGE is specified, the record is presented on the logical page
before or after (depending on the phrase used) the device is repositioned to
the next logical page. If the record to be written is associated with a record
sequential file whose file description entry contains a LINAGE clause, the
device is repositioned to the first line that can be written on the next
logical page as specified in the LINAGE clause.
- The phrases ADVANCING PAGE and END-OF-PAGE must not both be specified
in a single WRITE statement.
Formats 1, 2, and 3 (Sequential
Files)
- The associated file must be open in the OUTPUT or EXTEND mode at the
time of the execution of this statement. (See the topic
The OPEN
Statement.)
- The logical record released by the execution of the WRITE statement is
no longer available in the record area unless the associated file is named in a
SAME RECORD AREA clause or the execution of the WRITE statement was
unsuccessful due to a boundary violation.
The logical record is also available as a record
of other files referenced in the SAME RECORD AREA clause as the associated
output file, as well as the file associated with record-name.
Format 2 (Record
Sequential Files)
When the AFTER POSITIONING phrase is used in a WRITE
statement, the system moves a suitable character into the first position of the
record before it is written to the file. This first character position must be
reserved for this purpose. If the identifier-2 option is used, then the
character moved into the output record is simply the value held by identifier-2
and should be one of the following:
Identifier-2 | Interpretation |
---|
(space) |
Single-spacing |
0 |
Double-spacing |
- |
Triple-spacing |
+ |
Suppress spacing |
1-9 |
Skip to channel 1 through 9, respectively |
A, B, C |
Skip to channel 10, 11, 12, respectively |
V, W |
Pocket select 1 or 2 |
If the integer-1 option is used, then the character placed in the
output record is determined as follows:
Integer | Output Character | Interpretation |
---|
0 |
1 |
Skip to channel 1 |
1 |
(space) |
Single-space |
2 |
0 |
Double-spacing |
3 |
- |
Triple-spacing |
The END-OF-PAGE phrase, if specified, is documentary.


Format
3 (Record Sequential Files)


When an attempt is made to write beyond the
externally defined boundaries of a sequential file, an INVALID KEY condition
occurs. When the INVALID KEY condition occurs, the execution of the WRITE
statement is unsuccessful; the contents of the record area are unaffected, and
the FILE STATUS data item, if any, of the associated file is set to a value
indicating the cause of the condition. Execution
proceeds according to the rules stated in The INVALID KEY Condition in
this chapter (see also the section
I-O Status
in the chapter Procedure Division). Format 4
(Relative and Indexed Files)
-
The associated file must be open in the OUTPUT, I/O or EXTEND
mode at the time of execution of this statement; an indexed file must not be
open in the I/O mode with sequential access mode. (See the topic
The File Control
Entry and the section
The OPEN
Statement.)
- The logical record released by the execution of the WRITE statement is
available in the record area only if the associated file is named in a SAME
RECORD AREA clause, or the execution of the WRITE statement is unsuccessful due
to an INVALID KEY condition.
The logical record is also available as a record
of other files referenced in the same SAME RECORD AREA clause as the associated
output file, as well as the file associated with record-name.
- When the INVALID KEY condition is recognized, the execution of the
WRITE statement is unsuccessful; the contents of the record area are
unaffected, and the FILE STATUS data item, if any, of the associated file is
set to a value indicating the cause of the condition. Execution
proceeds according to the rules stated in The INVALID KEY Condition in
this chapter (see also the section
I-O Status
in the chapter Procedure Division).
Format 4
(Relative Files)
- When a file is opened in the OUTPUT mode, the WRITE statement proceeds
as follows:
-
If access mode is sequential, the WRITE statement causes a
record to be released to the operating system. The first record has a relative
record number of one and subsequent records releasedhave relative record
numbers of 2, 3, 4, ... . If the RELATIVE KEY data item has been specified in
the file control entry for the associated file, the relative record number of
the record just released is placed into the RELATIVE KEY data item by the
operating system during execution of the WRITE statement.
-
If access mode is random or dynamic, the value of the
RELATIVE KEY data item must be initialized in the runtime element, prior to the
execution of the WRITE statement, with the relative record number, or be
associated with the record in the record area. That record is then released to
the operating system by execution of the WRITE statement.
- When a file is opened in I/O mode and access mode is random or dynamic,
the value of the RELATIVE KEY data item must be initialized by the runtime
element with the relative record number to be associated with the record in the
record area. Execution of a WRITE statement then causes the contents of the
record area to be released to the operating system.
- The INVALID KEY condition exists under either of the following
circumstances:
- When access mode is random or dynamic, and the RELATIVE KEY data
item specifies a record which already exists in the file:
- When an attempt is made to write beyond the externally defined
boundaries of the file.
Format 4 (Indexed Files)
- Execution of the WRITE statement causes the contents of the record area
to be released. The operating system uses the contents of the record keys so
that subsequent access of the record can be made via any of those specified
record keys.
- The value of the prime record key should be unique within the records
in the file.
- The data item specified as the prime record key must be set by the
runtime element to the desired value prior to the execution of the WRITE
statement.
- If sequential access mode is specified for the file, records must be
released to the operating system in ascending order of prime record key values.
- If random or dynamic access mode is specified, records can be released
to the operating system in any program-specified order.
-
When the ALTERNATE RECORD KEY clause is specified in the file
control entry for an indexed file, the value of the alternate record key can be
non-unique only if the DUPLICATES phrase is specified for that record key data
item. In this case, the operating system provides storage of records so that
when records are accessed sequentially, records with duplicate alternate record
keys are retrieved in the order in which they are released to the operating
system.
- The INVALID KEY condition exists under the following circumstances:
- When sequential access mode is specified for a file opened in
OUTPUT mode, and the value of the prime record key is not greater than the
value of the prime record key of the previous record, or:
- When the file is opened in OUTPUT or I/O mode, and the value of the
prime record key is equal to the value of a prime record key of a record
already existing in the file, or:
- When the file is opened in OUTPUT or I/O mode, and the value of an
alternate record key for which duplicates are not allowed equals the
corresponding data item of a record already existing in the file, or:
- When an attempt is made to write beyond the externally defined
boundaries of the file.
Transfer of control following the
successful or unsuccessful execution of the WRITE operation depends on the
presence or absence of the INVALID KEY and NOT INVALID KEY phrases. (See the
topic
The INVALID KEY
Condition.)