COBOL File Open

COBOL File Declaration COBOL File Processing

Open operation is to open a file before performing any kind of process on files like read, write or re-write etc.

Without opening a file, Programmer can’t able to perform any processing operations.


In the above syntax, OPEN-MODE is very important.

Based on the usage of files, below four modes are defined on files.

  1. INPUT : Files can be used for READ only.
  2. OUTPUT : Files can be used for WRITE only.
  3. I-O : Files can be used for READ/WRITE/RE-WRITE.
  4. EXTEND : Files can be used for adding the records to the file which has some data already in WRITE mode.

This mode is used only in sequential files to append the records. (add more records to the existing records without deletion of previous records)

EXTEND mode cannot be used when input file is empty.

Practical Example:


  IDENTIFICATION DIVISION.                                         
  PROGRAM-ID. SEQFILE.                                             
  ENVIRONMENT DIVISION.                                            
  INPUT-OUTPUT SECTION.                                           
      SELECT FILE1 ASSIGN TO DISK1.                               
      ORGANIZATION IS SEQUENTIAL                                   
      ACCESS MODE IS SEQUENTIAL                                    
      FILE STATUS IS WS-FS.                                        
  DATA DIVISION.                                                   
  FILE SECTION.                                                    
      RECORD CONTAINS 80 CHARACTERS.                               
      BLOCK CONTAINS 800 CHARACTERS.                               
      RECORDING MODE IS F.                                        
      DATA RECORD IS STD-REC.                                      
  FD  FILE1.                                                       
  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-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.                                              
      DISPLAY 'SEQUENTIAL FILE READING...'.                        
      OPEN INPUT FILE1.                                            
      PERFORM UNTIL EOF-SW                                         
         READ FILE1                                                
         AT END MOVE 'Y' TO WS-EOF-SW                              
         DISPLAY 'CURRENT RECORD : ' STD-REC                      
      CLOSE FILE1.                                                 
      DISPLAY STD-REC.                                             
      STOP RUN.                                                    

COBOL File Declaration COBOL File Processing