COBOL File Rewrite

COBOL File Write COBOL File Delete

Advertisements


REWRITE is mainly used to update the records which is already existed in the file/already written to the file.

REWRITE can only work when the file is opened in I-O mode.


Syntax:

REWRITE FILE-REC 
	[FROM ws-file-rec]
	[INVALID KEY Statements]
	[NOT INVALID KEY Statements]
END-REWRITE.

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.


REWRITE without FROM:

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

REWRITE with FROM:

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

Practical Example:


Below example covers File REWRITE processing


Code:


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. FILEIO.                                              
 ENVIRONMENT DIVISION.                                            
 INPUT-OUTPUT SECTION.                                            
 FILE-CONTROL.                                                    
     SELECT FILE1 ASSIGN TO DISK1.                                
     ORGANIZATION IS INDEXED.                                     
     ACCESS MODE IS RANDOM.                                       
     RECORD KEY  IS STD-NO.                                       
     FILE STATUS IS WS-FS.                                        
 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).                                
 WORKING-STORAGE SECTION.                                         
 77 WS-STD-NO           PIC 9(03).                                
 77 WS-STD-NAME         PIC X(20).                                
 77 WS-FS               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.                                              
     ACCEPT WS-STD-NO.                                            
     ACCEPT WS-STD-NAME.                                          
     MOVE WS-STD-NO    TO STD-NO.                                 
     DISPLAY 'INDEXED FILE READING...'.                           
     OPEN I-O FILE1.                                              
     READ FILE1                                                   
     AT END MOVE 'Y'     TO WS-EOF-SW.                            
     IF WS-FS = 00                                                
        MOVE WS-STD-NAME TO STD-NAME                              
        REWRITE STD-REC                                           
     ELSE                                                         
        DISPLAY ' RECORD NOT FOUND IN FILE...'                    
     END-IF.                                                      
     CLOSE FILE1.                                                 
     STOP RUN.                                                    


COBOL File Write COBOL File Delete

Advertisements