The CLOSE Statement

The CLOSE statement terminates the processing of reels/units and files, with optional rewind, lock or removal where applicable.

General Formats for Format 1 (Record Sequential Files)


*

General Formats for Format 2 ( MF Line Sequential, Relative and Indexed Files)


*

Syntax Rules for All Formats (All Files)

  1. The files referenced in the CLOSE statement need not all have the same organization or access.

Syntax Rules for Format 1 (Record Sequential Files)

  1. MF The REEL and UNIT phrases should be specified only for files which have had MULTIPLE REEL or MULTIPLE UNIT specified in their SELECT clause. If your run-time system does not recognize multi-unit files, the statements CLOSE REEL and CLOSE UNIT are "null" statements; that is, they are documentary only. It is important that no other files are open on the device(s) closed by a CLOSE REEL or CLOSE UNIT statement.

    The statements CLOSE REEL WITH LOCK and CLOSE UNIT WITH LOCK are treated as equivalent to CLOSE REEL FOR REMOVAL.

  2. The DISP option is applicable only to tape files.

    It is documentary only.

General Rules for All Formats (All Files)

  1. A CLOSE statement can be executed only for a file in an open mode.
  2. Following the successful execution of a CLOSE statement the record area associated with file-name-1 is no longer available. The unsuccessful execution of such a CLOSE statement leaves the availability of the record area undefined.
  3. If a file is in the open mode when a STOP RUN statement is executed, the fileis closed. If a file has been opened in a called program and not closed in that program prior to the execution of a CANCEL statement for the program, that file is closed.
  4. Following the successful execution of a CLOSE statement, all record or file locks held by the run unit on the closed file are released.
  5. The execution of the CLOSE statement causes the value of the I/O status associated with file-name-1 to be updated. (See the topic I-O Status in the chapter Procedure Division.)
  6. The results of executing each type of CLOSE statement for each category of file are summarized in the table below.
    Table 1. Relationship of Categories of Files and the Formats of the CLOSE Statement
    CLOSE Statement Format File Category
    Non-Reel/Unit Record Sequential Single-Reel/Unit Record Sequential Multi-Reel/Unit Non-record Sequential Single/ Multi-Reel/Unit
    CLOSE C C, G A, C, G C
    CLOSE WITH LOCK C, E C, E, G A, C, E, G C, E
    CLOSE WITH NO REWIND record sequential only ANS85 C, H B, C A, B, C X
    CLOSE REEL/UNIT record sequential only ANS85 F ANS85 F, G F, G X
    CLOSE REEL/UNIT FOR REMOVAL record sequential only ANS85 F ANS85 D, F, G D, F , G X
    CLOSE REEL/UNIT WITH NO REWIND record sequential only X X F, B X

    The definitions of the symbols in the table are given below. Where the definition depends on whether the file is an input, output or input-output file, alternate definitions are given; otherwise, a definition applies to input, output, and input-output files.

    A. Previous Reels/Units Unaffected
     
    • Input Files and Input-output Files:

      All reels/units in the file prior to the current reel/unit are processed except those reels/units controlled by a prior CLOSE REEL/UNIT statement. If the current reel/unit is not the last in the file, the reels/units in the file following the current one are not processed.

    • Output Files:

      All reels/units in the file prior to the current reel/unit are processed except those reels/units controlled by a prior CLOSE REEL/UNIT statement.

    B. No Rewind of Current Reel

    The current reel/unit is left in its current position.

    C. Close File
     
    • Input Files and Input-output Files (Sequential Access Mode):

      If the file is at the end and label records are specified for the file, the labels are processed according to the operating system label convention. The behavior of the CLOSE statement when label records are specified but not present, or when label records are not specified but are present, is undefined. If the file is at the end and label records are not specified for the file, label processing does not take place, but other closing operations dependent on the run-time system are executed. If the file is not at the end, the closing operations dependent on the run-time system are executed, but there is no ending label processing.

    • Input Files and Input-output Files (Random or Dynamic Access Mode); Output Files (Random, Dynamic or Sequential Access Mode):

      If label records are specified for the file, the labels are processed according to the operating system standard label convention. The behavior of the CLOSE statement when label records are specified but not present, or when label records are not specified but are present, is undefined. If label records are not specified for the file, label processing does not take place, but other closing operations dependent on the run-time system are executed.

    D. Reel/Unit Removal
     

    The reel or unit can be accessed again, in the proper order of reels or units in the file, if a CLOSE statement without the REEL or UNIT phrase has previously been executed for this file followed by the execution of an OPEN statement for the file.

    E. File Lock

    This file cannot be opened again during this execution of this run unit.

    F. Close Reel/Unit
     
    • Input Files and Input-output Files (Reel/Unit Media):
      1. If the current reel/unit is the last or only reel/unit for the file, no reel/unit swap takes place; the current volume pointer remains unchanged, and the file position indicator is set to indicate that no next reel/unit exists.
      2. If another reel/unit exists for the file, a reel/unit swap occurs, the current volume pointer is updated to point to the next reel/unit existing in the file, the standard beginning reel/unit label procedure is executed, and the file position indicator is set to one less than the number of the first record existing on the new current volume. If no data records exist for the current volume, another reel/unit swap occurs.
    • Output Files (Reel/Unit Media):

      The following operations take place:

      1. Execution of the standard ending reel/unit label procedure.
      2. A reel/unit swap.
      3. Execution of the standard beginning reel/unit label procedure.
      4. The next executed WRITE statement that references that file directs the next logical data record to the next reel/unit of the file.
    • Input Files, Input-output Files, and Output Files (Non-Reel/Unit Media):

      Execution of this statement is considered successful. The file remains in the open mode, the file position indicator remains unchanged, and no action takes place except as specified in General Rule 4.

    G. Rewind

    The current reel or analogous device is positioned at its physical beginning.

    H. Optional Phrases Ignored

    The CLOSE statement is executed as if none of the optional phrases is present.

    X. Illegal

    This is an illegal combination of a CLOSE option and a file category. The results at object time are undefined.

General Rules for Format 1 (Record SequentialFiles)

Note: Except where otherwise stated in the general rules below, the terms "reel" and "unit" are equivalent and completely interchangeable in the CLOSE statement. Treatment of sequential mass storage files is logically equivalent to the treatment of a file on tape or analogous sequential media.

General Rules for All Formats (All Files)

  1. In order to show the effect of various types of CLOSE statements as applied to various storage media, all files are divided into the following categories:
    1. Non-reel/unit. A file whose input or output medium is such that the concepts of rewind and reels/units have no meaning.
    2. Sequential single-reel/unit. A sequential file that is entirely contained on one reel/unit.
    3. Sequential multi-reel/unit. A sequential file that is contained on more than one reel/unit.
  2. If the OPTIONAL phrase has been specified for the file in the File-Control paragraph of the Environment Division and the file is not present, the standard end-of-file processing is not performed for that file.
  3. If a CLOSE statement without the REEL or UNIT phrase has been executed for a file, no other statement (except the SORT or MERGE statements with the USING or GIVING phrases) can be executed that references that file, either explicitly or implicitly, unless an intervening OPEN statement for that file is executed.
  4. The WITH NO REWIND and FOR REMOVAL phrases have no effect at object time if they do not apply to the storage media on which the file resides.
  5. If WITH LOCK is specified, the file cannot be reopened during the current execution of the run unit. Otherwise a normal CLOSE takes effect.

    (This option has no connection with the record or file locking used when sharing files.)

General Rules for All Formats (All Files)

  1. If a CLOSE statement has been executed for a file, no statement

    MF other than a DELETE FILE statement

    can be executed that references that file, either explicitly or implicitly, unless an intervening OPEN statement for that file is executed.

  2. If WITH LOCK is specified, the file cannot be reopened during the current execution of the run unit.