Monday 28 July 2014

COBOL OPEN Statement: Essential Concepts and Examples!

OPEN Statement in COBOL
COBOL OPEN Statement. 

The OPEN statement in COBOL is a fundamental aspect of file handling in COBOL programming. It establishes communication between the COBOL program and an external data file, allowing the program to access and manipulate the data stored in the file. 

In this article, we will dive into the details of the COBOL OPEN statement, including its syntax, usage, and various options available for accessing data files. Whether you are a seasoned COBOL programmer or just starting out, this article will provide valuable insights and information to enhance your understanding of the COBOL OPEN statement.

An Introduction to Open Statement in COBOL.

The COBOL Open statement is a crucial component of file handling in COBOL programming. It enables the program to connect to an external data file and access the information stored in it for reading, writing, or updating purposes. This statement is essential for data processing and management in COBOL programming.

The OPEN statement kickstarts the handling of files and determines the accessibility of the file for processing. The file must be physically present and recognized by the input-output control system to be considered available. 

The execution of the OPEN statement prepares the related record area for the program but does not obtain or release the first data record. If the FILE STATUS clause is included in the FILE-CONTROL entry, the related status key is updated upon the execution of the OPEN statement.

COBOL OPEN Statement Syntax. 

The basic syntax of the COBOL Open statement is as follows:

COBOL OPEN Statement.

COBOL OPEN Statement. 

Here, the file name is the name of the file that needs to be opened. The COBOL OPEN statement must be preceded by a File Description entry in the Data Division that describes the characteristics of the file being opened. The file description should include the file name, the type of file organization, the record description, and any other relevant information such as the access mode.

The COBOL Open statement can be used in three modes: Input mode, Output mode, and Input-Output mode. In Input mode, the program reads the data from the file. In Output mode, the program writes new data to the file or updates existing data. In the Input-Output mode, the COBOL program reads and updates data into the file. 

The COBOL Open statement also has options for specifying the access mode, such as SEQUENTIAL, RANDOM, DYNAMIC, and EXTEND. 

SEQUENTIAL access mode is used for sequential files, where data is read or written in sequential order. RANDOM access mode is used for indexed files, where data can be accessed in any order. DYNAMIC access mode is used for dynamic files, where data can be added or deleted dynamically. EXTEND access mode is used for extending the file size if it runs out of space.

The COBOL Open statement must be followed by a Close statement to close the file and release the resources used by the program.

COBOL OPEN Statement Example. 

Here is an example of a COBOL Open statement in a COBOL program:

IDENTIFICATION DIVISION.
 PROGRAM-ID. EMP004.
ENVIRONMENT DIVISION.
 SELECT EMPMAST  ASSIGN TO EMPMST
        ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
 FILE SECTION.
 FD EMPMAST.
 01 EMP-RECORD.
 02 EMP-ID         PIC X(10).
WORKING-STORAGE SECTION.
...
...
...
PROCEDURE DIVISION.
  OPEN INPUT EMPMAST
  PERFORM UNTIL END-OF-FILE
       READ EMPMAST
          AT END SET END-OF-FILE TO TRUE
          NOT AT END DISPLAY 'NO MORE REC TO PROCESS'
       END-READ
   END-PERFORM.
   CLOSE INPUT-FILE.
 STOP RUN.

In this example, the COBOL Open statement is used to open a file called "EMPMAST" for reading. The ORGANIZATION clause specifies the type of file organization, in this example, the file organization is SEQUENTIAL. 

The file is described in the File Section with a File Description entry named "EMPMAST". The file contains one record with one field named "EMP-ID". The program uses the PERFORM UNTIL loop to read the records in the file until the end of the file is reached. 

The DISPLAY statement is used to display the message if there are no more records in the file. Finally, the COBOL Close statement is used to close the file.

Conclusion.

In conclusion, the COBOL Open statement is a crucial component of file handling in COBOL programming. It enables the program to access and manipulate external data files, making it an essential tool for data processing and management.


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