Skip to content

CLOSE Statement

The CLOSE statement is used to CLOSE a file or files.

General Format:

       CLOSE { file-1 {REEL} [ FOR REMOVAL ] } ... 
                      {UNIT} 
                      [ WITH LOCK ] 
                      [ WITH NO REWIND ]

Syntax:

file-n is a file described in the File Section with an FD.

General Rules:

  1. The CLOSE statement may only be applied to files that are in an OPEN state.
  2. When a file is CLOSE'd, all record and file locks held by the file are released.
  3. The REEL, UNIT, FOR REMOVAL, and WITH NO REWIND clauses are checked for syntax, but are otherwise treated as commentary.
  4. The REEL, UNIT, FOR REMOVAL, and WITH NO REWIND clauses can only be used with files described with ORGANIZATION SEQUENTIAL.
  5. CLOSE file-1 WITH LOCK prevents file-1 from being OPEN’ed again in the same program instance.
  6. The CLOSE operation updates the FILE STATUS variable.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. CLOSE-1. 
       ENVIRONMENT DIVISION. 
       INPUT-OUTPUT SECTION. 
       FILE-CONTROL. 
           SELECT TAPEFILE ASSIGN TO "TAPE-1" 
           ORGANIZATION IS SEQUENTIAL 
           ACCESS IS SEQUENTIAL 
           FILE STATUS IS TAPEFILE-STAT. 
       DATADIVISION. 
       FILE SECTION. 
       FD TAPEFILE.
       01 TAPEFILE-RECORD PIC X(80). 
       WORKING-STORAGESECTION. 
       77 TAPEFILE-STAT PIC XX. 
       77 DUMMY PIC X. 
       PROCEDURE DIVISION. 
       MAIN. 
      *CLOSE { FILE-NAME {REEL} [ FOR REMOVAL ] } ... 
      *                  [UNIT} 
      *                  [ WITH LOCK ] 
      *                  [ WITH NO REWIND ] 
           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE . 

           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE WITH NO REWIND. 

           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE WITH LOCK. 

           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE REEL. 

           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE REEL FOR REMOVAL. 

           OPEN INPUT TAPEFILE. 
           CLOSE TAPEFILE UNIT. 

               OPEN INPUT TAPEFILE. 
               CLOSE TAPEFILE UNIT FOR REMOVAL. 

               DISPLAY "CLOSE-1 COMPLETE!" LINE 10 COL 10. 
               ACCEPT DUMMY LINE 10 COL 30. 
               STOP RUN.
Back to top