Summary -

In this topic, we described about the File Processing with detailed example.

Once the file is declared in the program, the file processing should be handled in PROCEDURE DIVISION.File processing like file reading, record writing, record modifying, record deletion etc. Below is the list of file processing statements -

StatementsDescription
READ Used to read the records from the file.
WRITE Used to write a record to the file.
REWRITE Used to update the records that are already existed in the file.
DELETE Removes a record from an indexed or relative file.
START Used to set the file pointer to read the next record.
READ NEXT Used to read the next records from the current reading position of the file.
READ PREV Used to read the previous records from the current reading position of the file.

Practical Example -

Below example covers two file processing commands

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.