FILE CLOSE Statement


  • CLOSE statement is used to terminate the file processing and release the resources used by the file.
  • Once the file's CLOSE statement is executed successfully, the record area associated with it is no longer available.
  • This statement specifies that the program has finished using the file.

Syntax -

CLOSE file-name [WITH LOCK].
Note! All statements coded in [ ] are optional.

Parameters -

  • file-name - Specifies an 8-character logical file name defined inside the program. Multiple files can be closed with a CLOSE statement.
  • WITH LOCK - Specifies that the program exclusively locks the file and can't be used until the current run unit execution is completed.

Points to note -

  • A CLOSE statement closes files previously opened.
  • Like all other file handling statements, CLOSE statement execution status can be captured by a variable defined in the FILE STATUS clause.
  • If the file is not open and the execution of a CLOSE statement is unsuccessful, the EXCEPTION or ERROR can route to the error routine using the FILE STATUS.
Note! If a file is not closed with the CLOSE statement, it is automatically closed at the end of program execution.

Examples -


Scenario1 - Closing EMPFILE.

 CLOSE EMPFILE.

Scenario2 - Closing multiple files (EMPFILE1, EMPFILE2).

 CLOSE EMPFILE1
       EMPFILE2.