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

CALL VERB | COBOL CALL STATEMENT | CALL Statement in COBOL.

COBOL CALL statement is often used to CALL the running unit subroutine or subprogram. The calling program is called the "main program" and the called program is called a "subprogram" or "subroutine"

In today's session, you'll learn the basics of CALL statements in COBOL and how you can use call statements to call subprograms. You'll also learn the different variants of COBOL CALL phrases such as CALL BY VALUE, CALL BY CONTENT, and CALL BY REFERENCE

Let's get started with today's COBOL tutorial on the CALL statement in COBOL.  

Agenda.
  • Introduction.
  • COBOL CALL Statement and Syntax. 
    • CALL BY REFERENCE.
    • CALL BY VALUE.
    • CALL BY CONTENT.
  • Static CALL Vs Dynamic CALLs.
  • CALL statement control flow. 
  • COBOL CALL statement example. 

Introduction. 

Large scale enterprise applications is a combination of many system and subsystems. Different computing platforms, such as IBM Mainframe or cloud, support these systems.

Mainframe application is a combination of tons of batch and online programs. These programs are labeled as the main program, subprogram, and a copybook on the essence of the features and business logic used in these programs.

Copybooks are often used to include the structure of the file or even some basic piece of logic for example - month calculation. COPY statement are used to include the copybook logic in the COBOL program.

CALL Statement in COBOL. 

There are no specific criteria for the main program and subprogram. But, as per rule, the first program is often treated as the main program and other programs consider as subprograms. 

COBOL CALL statement is used to transfer control from the main program to subprogram and once the logic is executed then control is transferred back to main-program. 

Important Point: 
  • A COBOL subroutine/subprogram typically left in the last used-state in next subsiquent call. But, if you specify INITAL attribute in the program then subroutine will be initialized everytime it is called. 
  • You should always use GOBACK or EXIT PROGRAM statement instead of STOP RUN in called program. Otherwsie, subprogram and main program processing will be terminated.  
Let's look at syntax of CALL statements in COBOL. 

COBOL CALL Statement Syntax. 

The syntax of COBOL Call statement is pretty simple and straight forwards. You have to use CALL keyword followed by subroutine name and parameters value. following is the simple variant of COBOL call statemement.  

CALL literal-1 [USING identifier-1 . . .]

Now, let's discuss complex variant of CALL statement. In this variant, you have more control over the data that is passed from main program to subprogram and vice versa. 

CALL Statement in COBOL syntax, CALL Statement syntax
COBOL CALL Statement Syntax.

In above syntax, call by reference, call by value and call by content are three different techniques of passing data from main program to subprogram. If you don't specify anything then By REFERENCE will be assumed. Let's discuss each parameters one by one. 

  • CALL BY REFERNCE: The subprogram refers to and processes the data items in storage of the calling program rather than working on a copy of the data.
  • CALL BY CONTENT: The calling program passes only the contents of the literal or identifier. 
  • CALL BY VALUE: The calling program passes the value of the data-items, instead of reference to the sending data item. The called program can change the parameter, but the change do not affect the argument in calling program
Static Vs Dynamic Call in COBOL.

COBOL CALL statement is divided into following category: 
  • STATIC CALL
  • DYNAMIC CALL
In the static CALL statement, the COBOL program and all called programs are part of the same load module. When control is transferred, the called program already resides in storage, and a branch to it takes place. Subsequent executions of the CALL statement make the called program available in its last-used state unless the called program has the INITIAL attribute. In that case, the called program and each program directly or indirectly contained within it are placed into their initial state each time the called program is called within a run unit.

In these forms of the CALL statement, the called COBOL subprogram is not link-edited with the main program. Instead, it is link-edited into a separate load module, and is loaded at run time only when it is required (that is, when called). The program-name in the PROGRAM-ID paragraph or ENTRY statement must be identical to the corresponding load module name or load module alias of the load module that contains the program.

COBOL CALL Statement control flow: 

In the following example you have two programs in a run unit. The main program is PGMDSP01 and the subroutine i.e. EXPTAX01. 

Main program call subroutine by using COBOL CALL statement. The main program pass EMP-SAL and subroutine return Tax on employee salary.  



COBOL BLANK WHEN ZERO Clause | BLANK WHEN ZERO Example.

COBOL BLANK WHEN ZERO
BLANK WHEN ZERO In COBOL



While working on various COBOL reporting project's, I have come across with situation where requirement is to display SPACE despite of printing ZERO. Well! COBOL has a solution for that as well. 

You can use BLANK WHEN ZERO Clause. The Blank When Zero clause turns a numeric field to spaces when its value is zero. 

In this COBOL tutorial, I will discuss explain about BLANK WHEN ZERO COBOL Clause which is used to display COBOL numeric data in specific format (for example Suppressing leading zeros in COBOL).

The BLANK WHEN ZERO clause specifies that an item contains nothing but spaces when its value is zero. It can be specified only for elementary numeric or numeric-edit ed items. 

These items must be described, either implicitly or explicitly, as USAGE IS DISPLAY. When the BLANK WHEN ZERO clause in COBOL is specified for a numeric item, the item is considered a numeric-edited item (i.e. COBOL numeric-edited items are classified as alphanumeric data items and they cannot be used for any arithmetic expression) 


When you code the Blank When Zero clause, a numeric field that contains a value of zero is changed to spaces. If, for example, a field is defined with a Picture of ZZZ.99, it will look like .00 if a value of zero is moved to it. If you use the BLANK WHEN ZERO Clause with it, though, the field will be converted to spaces. 


Syntax : 


[VALUE IS literal]
[BLANK WHEN ZERO ]

Examples of Picture, Value, and Blank When Zero clauses.

05 HD1-EDIT-DTE             PIC 99/99/99. 
05 DTL-YTD-SALES-TOT        PIC S9(7)V99       VALUE ZERO. 
05 TRL-YTD-SALES-TOT-NBR    PIC Z,ZZZ,ZZZ.99CR BLANK WHEN ZERO.

Couple of Important Points that should be keep in mind while using COBOL BLANK WHEN ZERO CLAUSE.

  • The BLANK WHEN ZERO clause must not be specified for level-66 or level-88 items. You cannot use numeric-edited items as sending operands in arithmetic expressions or in ADD, SUBTRACT, MULTIPLY, DIVIDE, or COMPUTE statements.


  • The BLANK WHEN ZERO clause must not be specified for an entry containing the PICTURE symbols S or *

mainframe jobs

Preparing for Mainframe job interview? Have a look to our top 100 mainframe interview questions? 


Created with Artisteer

COBOL Invalid Key Clause | INVALID KEY Phrase in COBOL | COBOL Invalid Key Example.

File handling in COBOL is remarkably convenient. COBOL READ statement is used to read data from the file, and the file could be a sequential file or VSAM. COBOL INVALID KEY Phrase or simply COBOL INVALID KEY clause is used to handle an exception while reading data from the file. In this article, you'll learn the basics of INVALID KEY in COBOL, and how to code COBOL INVALID Phrase while writing the COBOL program.  

Agenda.

  • Introduction.
  • What is the INVALID KEY phrase in COBOL?
  • COBOL INVALID KEY phrase Syntax.
  • COBOL INVALID KEY Example.
  • Conclusion.

Introduction.

File handling is an important feature of any programming language. In COBOL, file handling is exceptionally simple. There are only four statements (i.e. COBOL READ statement, COBOL WRITE statement, COBOL DELETE statement, and COBOL REWRITE statement)  that can be used to access/update data in the sequential file or indexed file, or VSAM cluster.

What is the INVALID KEY Phrase in COBOL?

The INVALID KEY clause can be used with either the READ, WRITE, DELETE, and REWRITE statement for indexed files; it ensures that file records have valid keys. 

Important Point:- A valid key means, the correct key value of the records that are present in the file.    

The following errors can be detected when INVALID KEY is coded with a WRITE statement: 


  • a key field that is not in sequence (i.e. Incorrect or Invalid).
  • a key field has a duplicate value (i.e. Duplicate key). 
  • a key field has a blank value (i.e. Null value). 


Whenever any of the above conditions are met, the INVALID KEY clause will be executed as an imperative statement.


Whenever you code a NOT INVALID KEY phrase, it is executed only if the statement completes successfully. The INVALID KEY and NOT INVALID KEY phrases are not executed when the operation fails for any other reason than INVALID KEY. Instead, the program passes control to the end of the statement after performing any associated ERROR declarative.



COBOL INVALID KEY phrase Syntax.

As mention earlier, INVALID KEY Clause can be used with the COBOL READ statement, COBOL REWRITE statement and COBOL WRITE statements. The following figure showcases the usage of the INVALID KEY Clause in all three statements.

    READ FILE-NAME
                INVALID KEY  imperative statements
                NOT INVALID KEY imperative statements
    END-READ.

 INVALID KEY in COBOL, COBOL WRITE INVALID KEY CLAUSE

COBOL INVALID KEY Example.

The following example demonstrates how the INVALID KEY clause can be used to trap error conditions. If a record with key-value as '10093' is not found in the file then the INVALID KEY imperative statement will be executed. Otherwise NOT INVALID KEY clause imperative statement will be executed. In a similar way, you can use the INVALID KEY clause in REWRITE, READ, WRITE and DELETE statements.

MOVE '10093'   INTO EMPID.
READ EMP-MAST  INTO WS-EMP-REC
     INVALID KEY
       DISPLAY 'RECORD NOT FOUND!'
     NOT INVALID KEY
       DISPLAY 'RECORD FOUND!'
END-READ.

Conclusion. 

Finally, this marks an end to the INVALID KEY Clause in COBOL. The INVALID KEY phrase in COBOL or COBOL INVALID KEY clause can be used with either the COBOL READ, COBOL WRITE, COBOL DELETE, and COBOL REWRITE statement for indexed files. In this article, you'll learn the basics of INVALID KEY in COBOL, and how to code COBOL INVALID Phrase while writing the COBOL program. Also, do check out COBOL LEVEL 88 Condition.


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

Understanding the COBOL File Status Clause: A Beginner's Guide!

FILE STATUS in COBOL
COBOL File Status Clause.


The COBOL FILE STATUS clause is an important aspect of file handling in COBOL programming. It provides information about the outcome of file processing operations, allowing programs to determine the status of files and take appropriate action.  In this article, we will delve into the basics of the FILE STATUS clause in COBOL, including its syntax, usage, and importance in error handling.

An Introduction to COBOL File Status. 

The FILE STATUS clause is an optional component of the FILE-CONTROL entry in COBOL and is used to assign a two-character status code to a specified data item. This code indicates the result of file processing operations, such as open, read, write, and close, and helps to ensure the reliability and integrity of data file processing. 

The FILE STATUS clause is a valuable tool for error handling in COBOL programming and is commonly used to ensure the integrity and reliability of data file processing.

COBOL FILE Status Syntax. 

The FILE STATUS clause can be used with a SELECT statement for determining the result of an input/output file operation. If an input or output error has occurred, the file STATUS filed indicates the specific type of error.

When the FILE STATUS clause is specified, the system moves a value into the two-byte alphanumeric data name defined in the WORKING-STORAGE SECTION after each input-output operation that explicitly or implicitly refers to this file.

Format :

FILE STATUS CODE in COBOL Syntax
COBOL FILE Status.

 It is a very good practice to code the FILE STATUS clause for every file.

COBOL File Status Code Example.

Here's a simple example of a COBOL program that checks the file status after opening a file and performing a READ operation:

IDENTIFICATION DIVISION.
PROGRAM-ID. EMP004.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT FILE-1 ASSIGN TO EMPMAST
          ORGANIZATION IS SEQUENTIAL 
          ACCESS MODE  IS SEQUENTIAL
          FILE-STATUS  IS WS-FLE-ST

DATA DIVISION.
FILE SECTION.
FD FILE-1.
   01 FILE-RECORD.
      05 FILE-FIELD1    PIC X(10).
      05 FILE-FIELD2    PIC 9(5).

WORKING-STORAGE SECTION.
   01 WS-FILE-STATUS.
      05 WS-FLE-ST PIC XX.

PROCEDURE DIVISION.
   OPEN INPUT FILE-1
   PERFORM UNTIL WS-FLE-ST NOT = '00'
      READ FILE-1
         AT END
            MOVE '10' TO WS-FLE-ST
         NOT AT END
            MOVE '00' TO WS-FLE-ST
...
...
      END-READ
   END-PERFORM
   CLOSE FILE-1
   DISPLAY 'File status code: ' WS-FLE-ST
   STOP RUN.

In this example, the FILE-1 is opened for input and the program reads records from the file until the end of the file is reached. The WS-FILE-STATUS-CODE variable is set to '00' when a record is successfully read from the file, and set to '10' when the end of the file is reached. Finally, the file is closed and the WS-FILE-STATUS-CODE is displayed. 

Conclusion.

In summary, the FILE STATUS clause in COBOL provides a mechanism for a program to check the status of a file after I/O operations. It allows the program to handle errors that may occur during file processing, such as end-of-file, invalid key, or file not found. Understanding and using the FILE STATUS clause is important for writing robust and reliable COBOL programs.


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™

COBOL File Status: A Comprehensive Guide | COBOL File Status Codes.

COBOL File Status Codes.


Introduction. 

COBOL, a powerful programming language with a long-standing history, continues to play a significant role in various industries. One essential aspect of COBOL programming is the File Status Clause, which allows developers to handle file input and output operations effectively. In this blog post, we will deep dive into the details and complexities of the COBOL File Status Clause, exploring its purpose, different codes, their meanings, and practical implementation. By addressing the top 5 questions related to the File Status Clause, we aim to equip developers with the knowledge needed to navigate common errors and optimize their COBOL programs.

What is File Status Code in COBOL?

COBOL File Status is a mechanism that allows COBOL programs to determine the outcome of file operations. When a file operation is performed, the system sets a file status code that indicates the success or failure of the operation. The file status code can be used to determine whether the operation was successful, and if not, the reason for the failure.

Exploring COBOL File Status Codes.

The COBOL File Status Codes form a comprehensive set of indicators that convey specific information about file operations. These codes can range from 00 to 99 and are essential in determining the nature of file-related errors, if any. By understanding the different types of COBOL File Status Codes, programmers gain valuable context to troubleshoot and resolve issues efficiently.

Top 5 Questions about the COBOL File Status Clause.

What is the COBOL File Status Clause?

We provide a comprehensive answer to this fundamental question, explaining the purpose and significance of the File Status Clause in COBOL programming.

What are the different types of COBOL File Status Codes?

The COBOL File Status Clause is a crucial feature in COBOL programming that enables developers to assess the success or failure of file input and output operations. It provides a mechanism for obtaining valuable information about the outcome of file operations, allowing programmers to take appropriate actions based on the status of the file.

What does each COBOL File Status Code mean?

Each COBOL File Status Code carries a specific meaning that provides valuable insights into the outcome of file operations. Understanding the implications of each code is essential for effective error handling and resolution. Here are the explanations for some common COBOL File Status Codes:

  • 00: Successful completion without errors. The input or output operation was executed successfully.
  • 02: The file being processed has a duplicate alternate record key. This code indicates that a record with a duplicate key was encountered during processing.
  • 04: The length of the record being processed does not conform to the specifications defined in the File Description. This code suggests a mismatch between the expected and actual record length.
  • 10: A sequential READ statement (READ...AT END) was attempted, but there are no more input records available. This code indicates that there are no further records to read from the file.
  • 22: An attempt was made to write a record that would create a duplicate primary record key. This code implies that the record being written has a key value that already exists in the file.
  • 23: The required record was not found during a READ operation. This code suggests that the record being searched for is missing from the file.
  • 30: A permanent data error has occurred, indicating a hardware problem. This code signifies an issue with the physical data storage or retrieval hardware.
  • 34: A boundary error has occurred within a sequential file. It suggests an attempt to read or write beyond the pre-established boundaries of the file.
  • 37: An OPEN statement has been attempted on a file that does not support the specified mode. For example, an indexed file is opened as OUTPUT when ACCESS IS RANDOM has been specified. This code indicates an incompatibility between the specified mode and the file type.
  • 43: An attempt has been made to DELETE or REWRITE a record after an unsuccessful READ operation. This code suggests that there is no record in storage to delete or rewrite.
These are just a few examples of COBOL File Status Codes and their meanings. It is crucial to consult the COBOL documentation or the specific system's documentation for a comprehensive list of File Status Codes and their corresponding explanations.

Note: If the leftmost character in the FILE STATUS is a 0, the input or output operation was successfully completed. If it is not zero, then the I/O operation results in an error.

How do I use the COBOL File Status Clause in my program?

To utilize the COBOL File Status Clause effectively in your program, follow these steps:

  1. Declare the File Status Key: In the File Section of your COBOL program, define a data item to serve as the File Status Key. Typically, this is a two-character alphanumeric field, although its length may vary depending on the implementation. Ensure that this data item is associated with the relevant file in your program.
  2. Perform File Operations: Throughout your program, perform file input and output operations using appropriate file control statements such as OPEN, CLOSE, READ, WRITE, and DELETE. These statements specify the target file and the desired operation.
  3. Check the File Status: After each file operation, examine the value of the File Status Key to determine the outcome of the operation. This is typically done using an IF or EVALUATE statement to evaluate the File Status Key against specific File Status Codes.
  4. Handle File Errors: Based on the value of the File Status Key, implement error handling logic to address any encountered errors. This may involve displaying error messages, taking corrective actions, or terminating the program gracefully.

Here's an example illustrating the usage of the COBOL File Status Clause:

000100 IDENTIFICATION DIVISION.
000200  PROGRAM-ID. DEMOCL2B.
000300  AUTHOR. TOPICTRICK.
000400  DATE-WRITTEN.  01-MAR-2023.
000500  DATE-COMPILED. 01-MAR-2023.
000600*           
003200 ENVIRONMENT DIVISION.
003300  INPUT-OUTPUT SECTION.
003400  FILE-CONTROL.
003500     SELECT EMP-LIST ASSIGN TO  EMPLST
003600            ORGANIZATION IS SEQUENTIAL.
003700*
003800     SELECT EMP-FILE ASSIGN TO  EMPCUR
003900            ORGANIZATION IS INDEXED
004000            ACCESS       IS RANDOM
004100            RECORD KEY   IS IN-EMPNO
004200            FILE STATUS  IS WS-EMPL-ST.
004300*
004400     SELECT REP-FILE ASSIGN TO EMPRPT
004500            ORGANIZATION IS SEQUENTIAL.
004600*
004700 DATA DIVISION.
004800  FILE SECTION.
004900  FD EMP-LIST.
005000  01 EMP-LIST-REC.
        .....
        .....
006900*
007000  FD EMP-FILE.
007100  COPY EMPRECC REPLACING ==(EP)==  BY ==IN==.
007200*
007300  FD REP-FILE.
007400  01 REP-FILE-REC            PIC X(150).
007500*
007600 WORKING-STORAGE SECTION.
007700*
007800  01 WS-SWITCH.
007900     05 END-OF-FILE-SWITCH    PIC X(01)  VALUE 'N'.
008000        88 END-OF-FILE                   VALUE 'Y'.
008100        88 NOT-END-OF-FILE               VALUE 'N'.
      .....
      .....
009000*
009100  01 WS-FILE-STATUS-CDE.
009200     05 WS-EMPL-ST            PIC 9(02) VALUE ZEROES.
009300*
       ......
       ......

021100 PROCEDURE DIVISION.
021200 0000-CORE-BUSINESS-LOGIC.
021300     PERFORM A000-INIT-VALS
021400     PERFORM B000-OPEN-FILE
021500     PERFORM C000-PRNT-HDRS
021600     PERFORM D000-PROCESS-RECDS
021700     PERFORM X000-CLSE-FILE
021800     STOP RUN.
021900*
022000 A000-INIT-VALS SECTION.
022100 A010-INIT-TMP-VALS.
022200     INITIALIZE WS-COUNTERS, DTL-LINE, TRL-LINE,
022300                WS-TEMP-DATE.
022400*
022500 A099-EXIT.
022600      EXIT.
022700*
022800 B000-OPEN-FILE SECTION.
022900 B010-OPEN-FILE.
023000      OPEN INPUT  EMP-LIST
023100           I-O    EMP-FILE
023200           OUTPUT REP-FILE
023300           IF WS-EMPL-ST > 04
023400              PERFORM Z000-ABEND-RTN
023500           END-IF.
023600 B099-EXIT.
023700      EXIT.
023800*
      ..........
      .........
      ........

Note: In this example, the File Status Key, FILE-STATUS-KEY, is checked after each file operation (OPEN, READ, and CLOSE). Depending on the value of the File Status Key, appropriate actions are taken, such as displaying relevant messages or terminating the program if an error occurs.

What are some common errors that can occur with the COBOL File Status Clause?

There are a number of common errors that can occur with the COBOL File Status Clause. The following are common errors:
  • Not declaring a file status variable - If a file status variable is not declared, the system will not be able to return the file status code to the program.
  • Not checking the file status code - If the file status code is not checked, the program will not be able to determine whether the file operation was successful or not.
  • Using the wrong file status variable - If the wrong file status variable is used, the program will not be able to determine the correct status of the file operation.

Summary.

The COBOL File Status Clause is a powerful tool that can be used to determine the success or failure of file operations. By using the COBOL File Status Clause, programs can avoid errors and ensure that file operations are performed correctly.


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

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

New In-feed ads