FILE CLOSE Statement


The CLOSE statement is used to terminate the file processing and release the resources of the file.

The record area associated with the file is no longer available once the CLOSE statement on the file is executed successfully. 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 8-character logical file name defined inside the program. Multiple files can be closed with CLOSE statement.
  • WITH LOCK - Specifies the file can't be opened again until the current run unit execution is completed.

Notes -

  • A CLOSE statement used to close files that were previously opened using the OPEN statement.
  • Like all other file handling statements, CLOSE statement execution status can be captured by a variable defined in the FILE STATUS clause.
  • Suppose the file is not open and the execution of a CLOSE statement is unsuccessful. In that case, the EXCEPTION or ERROR can route to the error routine using the FILE STATUS.
  • CLOSE statement to ensure that all data has been written from buffers to the physical disk or storage medium.
Note! If any file is not closed with the CLOSE statement, the file gets automatically closed at the end of program execution.

Examples -


Scenario1 - Closing EMPFILE.

 CLOSE EMPFILE.

Scenario2 - Closing multiple files (EMPFILE1, EMPFILE2).

 CLOSE EMPFILE1
       EMPFILE2.