Summary -

In this topic, we described about the below sections -

The CLOSE statement terminates the processing of volumes and files.A CLOSE statement can be executed only for a file in an open mode.

After successful execution of a CLOSE statement, the record area associated with the file-name is no longer available. Unsuccessful execution of a CLOSE statement leaves availability of the record data undefined.

If the FILE STATUS clause is specified in the file-control entry, the associated file status key is updated with the status code when the CLOSE statement is executed.

If the file is in an open status and the execution of a CLOSE statement is unsuccessful, the EXCEPTION/ERROR procedure for the file is executed.

Syntax - CLOSE statement for sequential, indexed and relative files -


file-name-1 -

Specifies the file name. More than one file-name can be specified where the files need not have the same organization or access.

file-name-1 should not be a sort or merge file. If the SELECT OPTIONAL clause is specified in the file-control entry, the file is not available at run time and end-of-file processing is not performed.

File are divided into below types -

Sequential single volume -

A sequential file stored entirely on one volume. More than one file may be contained on this volume.

All VSAM files are single volume. QSAM files can be single volume.

Sequential multivolume -

A sequential file stored on more than one volume. QSAM files are the only files that can be multivolume.

Note! If any file is not closed explicitly with CLOSE statement, the file gets automatically closed at the end of program execution.

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.