The WRITE statement adds a record to a file.
Format 1
WRITE record-name [ FROM source ]
[ {BEFORE} ADVANCING { number [LINE ] } ]
{AFTER } { [LINES] }
{ PAGE }
{ mnemonic-name } (HP COBOL)
[ AT {END-OF-PAGE} statement-1 ]
{EOP }
[ NOT AT {END-OF-PAGE} statement-2 ]
{EOP }
[ END-WRITE ]
Format 2
WRITE record-name [ FROM source ]
[ INVALID KEY statement-1 ]
[ NOT INVALID KEY statement-2 ]
[ END-WRITE ]
Format 3
WRITE record-name [ FROM source ] WITH NO { CONTROL }
{ CONVERSION }
Syntax Rules
- record-name is the name of a record associated with a file described in the File Section of the Data Division. The associated file may
not be a sort file.
- source is a data item or literal. It may not share any storage area with record-name.
- number is an integer numeric literal or data item. It must be non-negative.
- Mnemonic-name is a user-defined word that may be assigned to Special Names in the ADVANCING clause of the WRITE statement.
(This is a feature of HP COBOL. See Special-Names Paragraph in
Transitioning to ACUCOBOL-GT for more information.
- statement-1 and
statement-2 are imperative statements.
- A Format 1 WRITE statement must be associated with a sequential file. A Format 2 WRITE statement must be associated with a
relative or indexed file.
- The words END-OF-PAGE and EOP are equivalent.
- If the END-OF-PAGE phrase is used, the file description entry containing record-name must have a LINAGE clause.
- The ADVANCING PAGE and END-OF-PAGE phrases cannot both be used in the same WRITE statement.
- A Format 3 WRITE statement must be associated with a sequential file.
General Rules
- The file associated with record-name must be open when the WRITE statement executes. For sequential access mode files, the
file must be open in the OUTPUT or EXTEND modes. For random and dynamic access mode files, the file must be open in the OUTPUT,
I-O, or EXTEND mode.
- The WRITE statement adds the contents of record-name to the file according to the following rules:
- For sequential access mode files, the record is added to the end of the file. If the file is indexed, the record's primary
key must contain a value that is larger than all of the primary keys currently in the file. If a relative file has a RELATIVE
KEY data item specified for it, the record number of the added record is moved to this data item when the WRITE statement
completes.
- For random and dynamic access mode files, the record is inserted into the file according to its key value. For relative files,
the record is placed at the record number described by the file's RELATIVE KEY data item. For indexed files, the values of
record-name's key items are used to insert the record in the file to maintain the correct key orderings.
- If the FROM phrase is used, the source item is moved to record-name according to the rules of the MOVE statement before record-name
is written to the file.
- The FILE STATUS data item is updated by the WRITE statement.
- Some sequential files are considered to be print files. A print file has page positioning information specified in it along
with the record data. A file with the PRINT option of the ASSIGN clause specified for it is a print file. A file that is referenced
by any WRITE statement that contains an ADVANCING phrase is also a print file.
- If the ADVANCING phrase is specified, the file is treated as a print file and the following occurs:
- If number is positive, the representation of the printed page is advanced a number of lines equal to that value.
- If number is zero, no repositioning of the representation of the printed page is performed.
- If the PAGE phrase is used, the representation of the printed page is advanced to the next page boundary. If the associated
file has a LINAGE clause specified for it, this is done by spacing the appropriate number of lines. Otherwise this is done
by physically advancing the device to the top of the next physical page.
- If the BEFORE phrase is specified, the page advancement specified occurs after
record-name is added to the file.
- If the AFTER phrase is used, the page advancement specified occurs before record-name is added to the file.
- If no ADVANCING phrase is specified, and the file is a print file, AFTER ADVANCING 1 LINE is implied.
- The invalid key condition exists when any of the following occur:
- A relative file record is written in the random or dynamic access modes, and the record number indicated by the RELATIVE KEY
data item is already used by another record.
- An indexed file record is written and the primary key value in
record-name is used by another record already in the file.
- An alternate key value in
record-name is already being used by a record in the file, and that alternate key does not allow for duplicates.
- When the invalid-key condition exists, the WRITE statement is unsuccessful and the following occurs:
- If the INVALID KEY phrase is specified,
statement-1 executes; otherwise
- If an appropriate USE AFTER EXCEPTION procedure exists, that error procedure executes; otherwise
- A message is printed and the program halts.
- If the WRITE statement is successful and the NOT INVALID KEY phrase is specified,
statement-2 is executed.
- The ordering of indexed file keys for alternate keys that allow duplicates is the order in which the records are written to
the file for those duplicated values.
- The current file position is not modified by the WRITE statement.
- The logical size of the record written to the file is the size of record-name. The physical size may be different due to physical
characteristics of the file.
- If the file associated with
record-name has a LINAGE clause, the following rules apply:
- An automatic page overflow condition occurs when the WRITE statement cannot be fully accommodated in the page body. This occurs
when the WRITE statement would cause the LINAGE-COUNTER to exceed the number of lines in the page body specified by the LINAGE
clause. When this happens, the line is presented before or after (depending on the phrase used) the device is positioned to
the first line of the next logical page.
- An end-of-page condition occurs when the WRITE statement causes printing or spacing in the footing area of the page body.
This occurs when the WRITE statement causes the LINAGE-COUNTER to equal or exceed the value of the FOOTING phrase of the associated
LINAGE clause. If no FOOTING phrase is present, then the end-of-page condition cannot occur. Note that the end-of-page condition
does not imply any automatic device positioning.
- If the END-OF-PAGE phrase is used, then
statement-1 executes if either an automatic page overflow or an end-of-page condition exists. Otherwise
statement-2 executes (if specified).
- The WRITE statement removes trailing spaces from
record-name if the file specifies trailing-space suppression. For related information, see:
- A Format 3 WRITE statement writes the data in record-name to its file without any additional carriage-control information.
In addition, if the NO CONVERSION option is specified, no trailing spaces are removed from the record, even if they otherwise
would be. Use this to send information to devices when carriage-control is inappropriate, for example, when sending a form
to a laser printer.
- Depending on the host environment, it is possible that records written with a Format 3 WRITE statement cannot later be retrieved
with a READ statement.
For configuration variables related to the WRITE statement, see: