COBOL File Close

COBOL File Read Next Difference between files

Advertisements


CLOSE statement used to close the files once the processing of the file completed.

An Opened file should be closed and that can be done by using CLOSE statement.

If any of the Opened file not closed, then the execution completion of the program will close all the files which are used in the program.


Syntax:

CLOSE Filename.

CLOSE command coding to close the file is a good practice.

At end of the Files topic, the basic steps that need to follow to use the files are:


  • Declare a file can be in INPUT OUTPUT section of ENVIRONMENT DIVISION.
  • Describe a file can be in FILE-SECTION of DATA DIVISION.
  • Open, process, close a files can be specify in PROCEDURE DIVISION.

When ORGANIZATION clause is not specified, by default file will be created as SEQUENTIAL FILE.


Practical Example:

Code:


  IDENTIFICATION DIVISION.                                         
  PROGRAM-ID. SEQFILE.                                             
  ENVIRONMENT DIVISION.                                            
  INPUT-OUTPUT SECTION.                                            
  FILE-CONTROL.                                                    
      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                       
      END-PERFORM.                                                 
      CLOSE FILE1.                                                 
      DISPLAY STD-REC.                                             
      STOP RUN.                                                    


COBOL File Read Next Difference between files

Advertisements