WRITE Statement COBOL

The WRITE statement releases a logical record for an output or input-output file. For a sequential file, it can also be used for vertical positioning of lines within a logical page.

Format 1:Write Sequential File

Write Sequential File

Format 2:Write Relative and Indexed File

Write Relative and Indexed File

In a Format 1 WRITE statement, record-name-1 must refer to a record associated with a sequential organization file.

In a Format 2 WRITE statement, record-name-1 must refer to a record associated with a relative or indexed organization file.

record-name-1 and identifier-1 must not reference the same storage area.

record-name-1 is the name of a logical record in the File Section of the Data Division and may be qualified.

When identifier-2 is used in the ADVANCING phrase, it must be the name of an elementary integer data item.

integer-1 or the value of the data item referenced by identifier-2 may be zero.

identifier-3 must reference an unsigned integer data item.

In a Format 2 WRITE statement, the INVALID KEY phrase must be specified if an applicable USE procedure is not specified for the associated file.

If the access mode is sequential, the associated file must be open in the OUTPUT or EXTEND mode at the time of the execution of this statement.

If the access mode is random or dynamic, the associated file must be open in the OUTPUT or I-O mode at the time of the execution of this statement.

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.

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 required 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 in the program.

The execution of the WRITE statement releases a logical record to the operating system. The contents of the record area are not changed.

When an attempt is made to write beyond the externally defined boundaries of a sequential file, an exception condition exists. The following action takes place:

  • The value of the file status data item, if any, of the associated file is set to a value indicating a boundary violation.
  • If a USE declarative is explicitly or implicitly specified for the file, that declarative procedure is executed.
  • If a USE declarative is not explicitly or implicitly specified for the file, an error message is displayed and the run unit is terminated.

When a relative file is opened in the output mode, records may be placed into the file by one of the following:

  • If the access mode is sequential, the WRITE statement causes a record to be released to the associated file. The first record has a relative record number of 1, and subsequent records have relative record numbers 2, 3, 4, If a 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 runtime system during execution of the WRITE statement.
  • If the access mode is random or dynamic, prior to the execution of the WRITE statement the value of the relative key data item must be initialized in the program with the relative record number to be associated with the record in the record area. That record is then released to the associated file by execution of the WRITE statement.

When a relative file is opened in the I-O mode and the access mode is random or dynamic, records are to be inserted in the associated file. The value of the relative key data item must be initialized by the program 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 associated file.

For an indexed file, the data item specified as the prime record key must be set by the program to the desired value prior to the execution of the WRITE statement. Records may be placed into the file by one of the following:

  • If the access mode is sequential, records must be released in strictly ascending order of prime record key values according to the collating sequence of the file, except that, if the DUPLICATES phrase is specified in the RECORD KEY clause, records with duplicate prime record key values may be released. If the access mode is random or dynamic, records may be released to the system in any program-specified order.
  • When the DUPLICATES phrase is specified for a record key of an indexed file, the value of the record key may be nonunique. In this case, the indexed file provides storage of records such that when records are accessed sequentially, the order of retrieval of those records is the order in which they are released to the runtime system.

In single record locking modes any record lock held by the run unit for the file associated with record-name-1 is released upon execution of the WRITE statement.

In multiple record locking modes any record locks held by the run unit for file-name-1 are not released upon execution of the WRITE statement.

FROM Phrase

FROM Phrase

The result of the execution of the WRITE statement with the FROM phrase is equivalent to the execution of a move from identifier-1 or literal-1 to record-name-1 followed by 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.

ADVANCING Phrase

ADVANCING Phrase

The ADVANCING phrase allows control of the vertical positioning of each line on a representation of a printed page. If the ADVANCING phrase is not used, automatic advancing occurs as if the user 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, which must be positive or zero.
  • If integer-1 is specified, the representation of the printed page is advanced the number of lines equal to the value of integer-1.
  • When mnemonic-name-2 is used, the name must be identified with a feature-name that is a channel-name in the SPECIAL-NAMES paragraph of the Environment Division. The representation of the printed page is advanced to the next occurrence of the channel indicator for the channel number associated with mnemonic-name-2. If the print device does not support channel skipping, advancing defaults to ADVANCING 1 LINE. The mnemonic-name phrase may not be used when writing a record to a file whose file description entry contains a LINAGE clause.
  • If the BEFORE phrase is used, the line is presented before the representation of the printed page is advanced.
  • If the AFTER phrase is used, the line is presented after the representation of the printed page is advanced.
  • 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 TO LINE phrase without the NEXT PAGE phrase is specified, the representation of the printed page is positioned to the line within the current page body corresponding to integer-2 or the value of the data item referenced by identifier-3.
  • If the TO LINE phrase with the NEXT PAGE phrase is specified, the representation of the printed page is positioned to the line within the next logical page body corresponding to integer-2 or the value of the data item referenced by identifier-3.
  • If PAGE is specified and the LINAGE clause is specified in the associated file description entry, 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. The repositioning is to the first line that can be written on the next logical page as specified in the LINAGE clause.
  • If PAGE is specified and the LINAGE clause is not specified in the associated file description entry, the record is repositioned to the next physical page. If physical page has no meaning in conjunction with a specific device, advancing occurs as if the user had specified BEFORE or AFTER (depending on the phrase used) ADVANCING 1 LINE.

END-OF-PAGE and NOT END-OF-PAGE Phrases

END-OF-PAGE and NOT END-OF-PAGE Phrases

If the END-OF-PAGE phrase, the NOT END-OF-PAGE phrase or the ADVANCING TO LINE phrase is specified, the LINAGE clause must be specified in the file description entry for the associated file.

The words END-OF-PAGE and EOP are synonymous.

An end-of-page condition occurs when the execution of a WRITE statement with the END-OF-PAGE phrase causes printing or spacing within the footing area of a page body. This occurs when the execution of such a WRITE statement causes the LINAGE-COUNTER to equal or exceed the value specified by integer-8 or the data item referenced by data-name-5 of the LINAGE clause. In this case, the WRITE statement is executed and then imperative-statement-1 in the END-OF-PAGE phrase is executed.A NOT END-OF-PAGE phrase, if present, is ignored.

An automatic page overflow condition occurs when the execution of a WRITE statement (with or without an END-OF-PAGE phrase) cannot be fully accommodated within the current page body. An automatic page overflow condition does not occur as a result of the execution of a WRITE statement containing a NEXT PAGE phrase. An automatic page overflow condition occurs when the execution of a WRITE statement causes the LINAGE-COUNTER to exceed the value specified by integer-7 or the data item referenced by data-name-4 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. imperative-statement-1 in the END-OF-PAGE phrase, if specified, is executed after the record is written and the device has been repositioned.

A page overflow condition occurs when the execution of a WRITE statement causes the LINAGE-COUNTER to simultaneously exceed the value of both integer-8 and the data item referenced by data-name-5 of the LINAGE clause and integer-7 or the data item referenced by data-name-4 of the LINAGE clause.

If the execution of a WRITE statement with the TO LINE phrase would cause the record to be presented on a line outside the current page body if the NEXT PAGE phrase is not specified, or outside the next page body if the NEXT PAGE phrase is specified, the execution of the WRITE statement is unsuccessful.Furthermore, if the execution of the WRITE statement with identifier-2 or integer-1LINES phrase would cause the LINAGE-COUNTER associated with record-name-1 to have a negative or zero value, the execution of the WRITE statement is unsuccessful. If the execution of the WRITE statement is unsuccessful for one of these reasons, an exception condition exists, the contents of the record area and of LINAGE-COUNTER are unchanged, and the following actions take place:

  • If the file with which record-name-1 is associated has a file status data item, its value is set to a value indicating a page boundary violation.
  • If a USE procedure is explicitly or implicitly specified for the file associated with record-name-1, that declarative procedure is executed.
  • If a USE procedure is not explicitly or implicitly specified for the file associated with record-name-1 , control is transferred to the next executable statement.

INVALID KEY and NOT INVALID KEY Phrases

INVALID KEY imperative –statement-1

NOTINVALID KEY imperative-statement-2

The invalid key condition exists under one of the following circumstances:

  • When a relative file has random or dynamic access mode and the relative key data item specifies a record that already exists in the file.
  • When the access mode is sequential for an indexed file opened in the 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, except that, if the DUPLICATES phrase is specified in the RECORD KEY clause of the file control entry, the value of the prime record key may be equal to the value of the prime record key of the previous record.
  • When an indexed file is opened in the output or I-O mode, and the value of the prime record key is equal to the value of the prime record key of a record already existing in the file and the DUPLICATES phrase is not specified in the RECORD KEY clause of the file control entry.
  • When an indexed file is opened in the 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.
  • When an attempt is made to write beyond the externally defined boundaries of the file.

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. Transfer of control following the successful or unsuccessful execution of a Format 2 WRITE statement depends on the presence or absence of the optional INVALID KEY and NOT INVALID KEY phrases in the WRITE statement. This topic is presented in detail in the discussions of invalid key conditions.

WRITE Statement Examples


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics