WRITE statement is used to write a record to the file. WRITE statement releases a logical record to an output or input/output file. To execute the WRITE statement -

  • The sequential file must be open in OUTPUT or EXTEND mode.
  • The indexed or relative file must be open in OUTPUT, I-O or EXTEND mode.

Syntax-1: sequential files

WRITE Syntax for sequential files

Syntax-2: indexed and relative files

WRITE Syntax for indexed and relative files

record-name-1

Specifies the record name for the file. Must be defined in a data division FD entry. record-name-1 can be qualified. It must not be associated with a sort or merge file.

FROM phrase -

The result of WRITE statement execution with the FROM identifier-1 phrase is equivalent to the execution of the following statements in the order specified -

MOVE identifier-1 TO record-name-1. WRITE record-name-1.

identifier-1

identifier-1 can be -

  • A data item defined in the working-storage section or the linkage section
  • A record definition for another previously opened file

identifier-1 must be a valid sending item for a MOVE statement with record-name-1 as the receiving item. identifier-1 and record-name-1 must not refer to the same storage area. After the WRITE statement is executed, the information is still available in identifier-1.

identifier-2

Must be an integer data item.

ADVANCING phrase -

Controls positioning of the output record on the page.

Rules: -

When the ADVANCING phrase is specified, the following rules apply -

  • When BEFORE ADVANCING is specified, the line is printed before the page is advanced.
  • When AFTER ADVANCING is specified, the page is advanced before the line is printed.
  • When identifier-2 is specified, the page is advanced the number of lines equal to the identifier-2 value. identifier-2 must name an elementary integer data item. identifier-2 cannot name a date field.
  • When integer is specified, the page is advanced the number of lines equal to the integer value.
  • Integer or the value in identifier-2 can be zero.
  • When PAGE is specified, the record is printed on the logical page BEFORE or AFTER the device is positioned to the next logical page. If PAGE has no meaning for the device used, then BEFORE or AFTER ADVANCING 1 LINE is provided.

When the ADVANCING phrase is omitted, automatic line advancing is applicable as AFTER ADVANCING 1 LINE.

END-OF-PAGE phrase -

When END-OF-PAGE is specified and the logical end of the printed page is reached during execution of the WRITE statement, the END-OF-PAGE imperative-statement is executed.

The keywords END-OF-PAGE and EOP are equivalent. Both the ADVANCING PAGE phrase and the END-OF-PAGE phrase can be specified in a single WRITE statement.

INVALID KEY phrase -

An invalid key condition occurs when the following -

For sequential files -

  • An attempt is made to write beyond the externally defined boundary of the file.

For indexed files -

  • An attempt is made to write beyond the externally defined boundary of the file.
  • ACCESS SEQUENTIAL is specified, the file is opened OUTPUT and the value of the record key is not greater than the previous record key.
  • The file is opened OUTPUT or I-O and the value of the record key equals to an already existing record.

For relative files -

  • An attempt is made to write beyond the externally defined boundary of the file.
  • When the access mode is random or dynamic and the RELATIVE KEY data item specifies a record that already exists in the file.
  • The number of significant digits in the relative record number is larger than the size of the relative key data item for the file.

If the INVALID KEY phrase is specified, imperative-statement-1 is executed. The INVALID KEY conditions apply to a relative file opened in OUTPUT mode or EXTEND mode. If the NOT INVALID KEY phrase is specified and no error occurred, control is passed to imperative-statement-4.

END-WRITE phrase -

The END-WRITE used to end the scope of the UNSTRING statement. END-WRITE is not required when WRITE statement ended with period.END-WRITE can also be used with an imperative WRITE statement. END-WRITE permits conditional WRITE to be nested in another conditional statement.

Practical Example - FILE WRITE

Below example for copying one file data into another file.

Code:


IDENTIFICATION DIVISION.                                         
PROGRAM-ID. FILEWRT.                                             
ENVIRONMENT DIVISION.                                            
INPUT-OUTPUT SECTION.                                            
FILE-CONTROL.                                                    
    SELECT FILE1 ASSIGN TO DISK1.                                
    ORGANIZATION IS SEQUENTIAL                                   
    ACCESS MODE IS SEQUENTIAL                                    
    FILE STATUS IS WS-FS1.                                       
    SELECT FILE2 ASSIGN TO DISK2.                                
    ORGANIZATION IS SEQUENTIAL                                   
    ACCESS MODE IS SEQUENTIAL                                    
    FILE STATUS IS WS-FS2.                                       
DATA DIVISION.                                                   
FILE SECTION.                                                    
FD  FILE1.                                                       
    RECORD CONTAINS 80 CHARACTERS.                               
    BLOCK CONTAINS 800 CHARACTERS.                               
    RECORDING MODE IS F.                                         
    DATA RECORD IS STD-REC.                                      
01 STD-REC.                                                      
    02 STD-NO          PIC 9(03).                                
    02 STD-NAME        PIC X(20).                                
    02 STD-GENDER      PIC X(07).                                
    02 FILLER          PIC X(50).                                
FD  FILE2.                                                       
    RECORD CONTAINS 80 CHARACTERS.                               
    BLOCK CONTAINS 800 CHARACTERS.                               
    RECORDING MODE IS F.                                         
    DATA RECORD IS STD-REC2.                                     
01 STD-REC2.                                                     
    02 STD-NO          PIC 9(03).                                
    02 STD-NAME        PIC X(20).                                
    02 STD-GENDER      PIC X(07).                                
    02 FILLER          PIC X(50).                                
WORKING-STORAGE SECTION.                                         
77 WS-FS1              PIC 9(02).                                
77 WS-FS2              PIC 9(02).                                
01 WS-EOF-SW           PIC X(01) VALUE 'N'.                      
   88 EOF-SW           VALUE 'Y'.                                
   88 NOT-EOF-SW       VALUE 'N'.                                
PROCEDURE DIVISION.                                              
    DISPLAY 'SEQUENTIAL FILE READING & WRITING..'                
    OPEN INPUT FILE1.                                            
    OPEN OUTPUT FILE1.                                           
    PERFORM UNTIL EOF-SW                                         
       READ FILE1                                                
       AT END MOVE 'Y' TO WS-EOF-SW                              
       MOVE STD-REC TP STD-REC2.                                 
       WRITE STD-REC2                                            
    END-PERFORM.                                                 
    CLOSE FILE1.                                                 
    CLOSE FILE2.                                                 
    STOP RUN.