COBOL File Write

COBOL File Read COBOL File Rewrite

Advertisements


Write Statement is used to write the provided record into the file.

Writing into the file can be done in two ways based on the file opening mode.

1. If the file opened in OUTPUT mode, the records will be writing from the first record onwards. If the file have some data before opening in OUTPUT mode, that can be refreshed and start writing from beginning onwards.

2. If the file opened in EXTEND mode, the records will be appending to the file from the last record onwards.


Syntax:

WRITE File-rec
	[FROM ws-file-rec]
	[INVALID KEY Statements]
	[NOT INVALID KEY statements]
END-WRITE.

FROM Clause avoids the explicit move of ws-file-rec to File-rec like below.

INVALID KEY and NOT INVALID KEY is used to validate the passed key is valid or not when the file is reading randomly.

INVALID KEY and NOT INVALID KEY option is available for other than indexed files.


WRITE without FROM:

MOVE ws-file-rec to File-rec
WRITE File-rec
 [INVALID KEY Statements]
[NOT INVALID KEY statements]
END-WRITE.

WRITE with FROM:

WRITE File-rec
[FROM ws-file-rec]
[INVALID KEY Statements]
[NOT INVALID KEY statements]
END-WRITE.

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.                                                    


COBOL File Read COBOL File Rewrite

Advertisements