Monday, 28 July 2014

COBOL CLOSE statement: When to close a file after it has been opened?

COBOL CLOSE Statement

COBOL CLOSE Statement.

In this article, we will explore the COBOL CLOSE statement and its significance in proper file management in your COBOL program. The COBOL CLOSE statement is typically used at the end of a program or section of code that has finished processing a file, after an error occurs while processing a file, or before a file is to be reopened with a different mode.

We will deep dive into the syntax of the CLOSE statement and the benefits of using it in COBOL programming. We will also discuss some best practices for using the CLOSE statement and troubleshooting common errors.

By the end of this article, you will have a deeper understanding of the COBOL CLOSE statement and its role in proper file management. You will be able to use the CLOSE statement effectively in your own COBOL programs and ensure that your files are properly managed and protected.

An Introduction to COBOL CLOSE Statement.  

As a fundamental part of the COBOL programming language, the COBOL CLOSE statement is used to close a file that has been previously opened for input, output, or both. Once a file is closed, the program can no longer access the file's data and any further operations on the file will result in an error. 

The CLOSE statement is an important step in properly managing file resources, as it allows the operating system to release any resources that were allocated to the file, such as memory and file handles.

The COBOL CLOSE statement is typically used at the end of a program or section of code that has finished processing a file, after an error occurs while processing a file, or before a file is to be reopened with a different mode. 

COBOL CLOSE Statement - Syntax. 

The CLOSE statement in COBOL is used to close a file that has been previously opened for input, output, or both. Once a file is closed, the program can no longer access the file's data and any further operations on the file will result in an error. 

The basic syntax of the CLOSE statement is:

CLOSE Statement in COBOL.
CLOSE Statement in COBOL.


CLOSE file-name [REEL/UNIT] [status-key].

Where "file-name" is the name of the file to be closed, "REEL/UNIT" is an optional clause that specifies the type of file, and "status-key" is an optional variable that can be used to check the status of the file after it has been closed.

The CLOSE statement is typically used in the following situations:
  • At the end of a program or section of code that has finished processing a file.
  • After an error occurs while processing a file.
  • Before a file is to be reopened with a different mode (for example, from input to output).
It's important to note that if you want to close all the files opened by your program, you can use the CLOSE ALL statement instead of closing each file individually. The STOP RUN statement automatically closes all the files that were opened by the program. The COBOL CLOSE statement can be used with the LOCK option, which prevents the file to be opened again in the same program.


How the CLOSE Statement is used in COBOL? 

The following example illustrates the usage of the Close statement in the COBOL program: 

  IDENTIFICATION DIVISION. 
  PROGRAM-ID. CLOSE-EXAMPLE.

* Declare the file we will be working with
  FILE-CONTROL.
   SELECT CUSTOMER-FILE ASSIGN TO CUSTMST
          ORGANIZATION  IS SEQUENTIAL.

* Open the file for input
  OPEN INPUT CUSTOMER-FILE.

* Read and process the file's data
* ...

* Close the file after we are done with it
  CLOSE CUSTOMER-FILE.

* End of program
  STOP RUN.

In the above example, we first declare a file called "CUSTOMER-FILE" and assign it to the "CUSTMAST" DD name. We then open the file for input using the OPEN INPUT statement. Next, the program reads and processes the data in the file. 

Once the program is done processing the file, it uses the CLOSE statement to close the file. This releases any resources that were allocated to the file, such as memory and file handles, and ensures that the data stored in the file is properly saved. Finally, the program ends with the STOP RUN statement.

COBOL CLOSE statement benefits.

The benefits of using the CLOSE statement in COBOL are as follows:

  • Proper Resource Management: By closing a file after it has been used, the operating system is able to release any resources that were allocated to the file, such as memory and file handles. This helps to ensure that resources are not wasted and that the system remains stable and efficient.
  • Data Integrity: Closing a file after it has been used ensures that any data that was written to the file is properly saved and protected from being overwritten or lost.
  • Error Detection: The CLOSE statement can also be used to check the status of a file after it has been closed. If an error occurs while closing a file, the status-key variable can be checked to determine the cause of the error, allowing for more accurate and efficient debugging.
  • Improved security: Closing a file after it has been used will prevent unauthorized access to the data stored in the file, ensuring that sensitive information is protected.
  • Better performance: Closing a file after it has been used will release memory and file handle resources that can be used to open and process other files, thus improving the overall performance of the system.
  • Reopening the file: Closing a file also allows one to open the same file again with a different mode such as Input, Output, I-O, etc.
Important points: The CLOSE Statement in COBOL is an option from COBOL-85. However, it was mandatory for the previous version of COBOL Compilers. 

Though it is possible to OPEN and CLOSE more than one file at a time, programmers are advised not to do so. This is because if the opening or closing of any particular file is unsuccessful, then it is impossible to identify that file using the FILE STATUS as it applies to all the files.

Conclusion. 

In summary, the CLOSE statement in COBOL is an important statement used to close a previously opened file. The purpose of the "CLOSE" statement is to release any resources associated with the file and make it unavailable for further processing. You can use the COBOL Close statement in your COBOL program as per business requirements. 


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

Created with Artisteer

No comments:

Post a Comment

New In-feed ads