The WRITE Statement

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.

XOPEN Although they are a part of the standard COBOL definition, mnemonic names in the ADVANCING phrase are explicitly excluded from the X/Open COBOL language definitions and should not be used in X/Open COBOL source programs.

General Formats for Format 1 (RecordMF  and Line Sequential Files)


Syntax for General Formats for Format 1 (Record for Micro Focus dialect and Line for all dialects) for the WRITE statement
OSVS 

General Formats for Format 2 (Record Sequential Files)


Syntax for General Formats for Format 2 (Record Sequential Files) for the WRITE statement
OSVSVSC2MF 

General Formats for Format 3 (Record Sequential Files)


Syntax for General Formats for Format 3 (Record Sequential Files) for the WRITE statement

General Formats for Format 4 (Relative and Indexed Files)


Syntax for General Formats for Format 4 (Relative and Indexed Files) for the WRITE statement

Directives and RTS switches

  1. In addition to Compiler directives which provide flagging and modify the reserved word list, the following directives may impact either the syntax or the semantics described in this section.
    • WRITE-LOCK - causes a WRITE statement to acquire a record lock when multiple recording locking is used.
    • FDCLEAR - causes the contents of the record area to be predictable after a WRITE statement.
  2. The following RTS switches may impact the semantics described in this section.
    • N - controls the insertion of null characters before control characters when writing line sequential records.
    • T - controls the insertion of tab characters when writing line sequential records.

Syntax Rules for All Formats (All Files)

  1. ANS85 If identifier-1 is a function-identifier, it must reference an alphanumeric function. When identifier-1 is not a function-identifier, record-name and identifier-1 must not reference the same storage area.
  2. Record-name is the name of a logical record in the File Section of the Data Division and can be qualified.
  3. OSVSVSC2MF Record-name can define a floating-point item.
  4. OSVSVSC2MF Identifier-1 can be defined as a floating-point item.

Syntax Rules for Format 1 (RecordSequential Files)

  1. MF A user-defined mnemonic-name can be used instead of TAB if they are associated in the Special-Names paragraph. (See the topic The Special-Names Paragraph.)
  2. When identifier-2 is used in the ADVANCING phrase, it must be the name of an elementary integer data item.
  3. Integer, or the value of the data item referenced by identifier-2, can be zero.
  4. VSC2 Integer can be signed.
  5. If the END-OF-PAGE phrase is specified, the LINAGE clause must be specified in the file description entry for the associated file.
  6. The words END-OF-PAGE and EOP are equivalent.
  7. The ADVANCING mnemonic-name phrase cannot be specified when writing a record to a file whose file description entry contains the LINAGE clause.
  8. ANS85 The phrases ADVANCING PAGE and END-OF-PAGE must not both be specified in a single WRITE statement.

    MF This restriction can be ignored.

    OSVSVSC2MF Alternatively, function-name can itself be used instead of an associated mnemonic-name.

OSVS 

Syntax Rules for Format 2 (Record Sequential Files)

  1. OSVS This format cannot be specified when writing a record to a file whose file description entry contains the LINAGE clause.
  2. OSVS If this format of the WRITE statement is used for writing to a given file, then every WRITE statement used for that file should be in this format.
  3. OSVS In the AFTER POSITIONING phrase, identifier-2 must be defined as a single-character alphanumeric item. See General Rule 18 for its possible values.

Syntax Rules for Format4 (Relative and Indexed Files)

  1. The INVALID KEY phrase must be specified if an applicable USE procedure is not specified for the associated file.

    OSVSVSC2MF This rule is not enforced.

General Rules for All Formats (All Files)

  1. The results of the execution of the WRITE statement with the FROM phrase is equivalent to the execution of:
    1. The statement:
       MOVE identifier-1 TO record-name

      according to the rules specified for the MOVE statement:

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

  2. The file position indicator is unaffected by the execution of a WRITE statement.
  3. 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.)
  4. The maximum record size for a file is established at the time the file is created and must not subsequently be changed.
  5. 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 .
  6. The execution of the WRITE statement releases a logical record to the operating system.
  7. ANS85 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.)

General Rules for Format 1 (Record SequentialFiles)

  1. MF When the mnemonic-name associated with TAB is specified the result is to cause the paper to throw to the standard vertical tabulation position.
  2. 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.
  3. 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.

MF 

General Rules for Format 1 (LineSequential Files)

  1. MF 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).
  2. MF  MF 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:
    1. 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.)
    2. If a USE AFTER STANDARD EXCEPTION declarative is explicitly or implicitly specified for the file, that declarative procedure is executed.
    3. If a USE AFTER STANDARD EXCEPTION declarative is not explicitly or implicitly specified for the file, the result is undefined.
  3. MF  MF 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:
    1. The standard ending reel/unit procedure.
    2. The reel/unit swap.
    3. The standard beginning of reel/unit label procedure.
  4. XOPEN 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.
  5. ANS85 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.
MF 

General Rules for Format1 (Record and Line Sequential Files)

  1. 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:

    1. 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.
    2. If integer is specified, the representation of the printed page is advanced the number of lines equal to the value of integer.
    3. If mnemonic-name is specified, the representation of the printed page is advanced as specified in the topic The Special-Names Paragraph.
    4. 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.
    5. 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.
    6. 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.
  2. The phrases ADVANCING PAGE and END-OF-PAGE must not both be specified in a single WRITE statement.

General Rules for Formats 1, 2, and 3 (Sequential Files)

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

OSVS 

General Rules for Format 2 (Record Sequential Files)

  1. OSVS 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
  2. OSVS The END-OF-PAGE phrase, if specified, is documentary.
OSVSVSC2MF 

General Rules for Format 3 (RecordSequential Files)

  1. OSVSVSC2MF 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).

General Rules for Format 4 (Relative and Indexed Files)

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

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

General Rules for Format4 (Relative Files)

  1. When a file is opened in the OUTPUT mode, the WRITE statement proceeds as follows:
    1. 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.
    2. If access mode is random or dynamic, the value of the RELATIVE KEY data item must be initialized in the run-time system 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.
  2. 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 run-time system 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.
  3. The INVALID KEY condition exists under either of the following circumstances:
    1. When access mode is random or dynamic, and the RELATIVE KEY data item specifies a record which already exists in the file:
    2. When an attempt is made to write beyond the externally defined boundaries of the file.

General Rules for Format 4 (Indexed Files)

  1. 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.
  2. The value of the prime record key should be unique within the records in the file.
  3. The data item specified as the prime record key must be set by the run-time system element to the desired value prior to the execution of the WRITE statement.
  4. 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.
  5. If random or dynamic access mode is specified, records can be released to the operating system in any program-specified order.
  6. 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.
  7. The INVALID KEY condition exists under the following circumstances:
    1. 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:
    2. 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:
    3. 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:
    4. When an attempt is made to write beyond the externally defined boundaries of the file.
  8. ANS85 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.)