COBOL Sample file Program – Sequential File Read

This sample program explains how to define a PS(Physical Sequential) File in different divisions and the steps to access it in the procedure division.

Steps:

  1. Environment Division –> Input-Output Section –> File-Control . Here COBOL File name is given to the PS file and a linking between the cobol program and PS file is established. It also defines the file attributes like Organization and Access mode. ‘FILE STATUS IS’ clause also defines here which gives a variable name to store the file status codes.
  2. DATA Division –> File Section –> FD <File Name> . Here the mandatory file Record gets defined. There are some optional parameters like LABEL RECORDS ARE , RECORD CONTAINS etc, but for this simple example those not included.
  3. Define the FILE Status Variable in working storage section.
  4. In Procedure Division, First Open the File. As this is to READ the data from the file, it is opened in INPUT mode. After the OPEN statement check whether the OPEN is successful OR failure by checking the FILE status variable.
  5. Read the file, As there can be multiple records in the file, kept this read in a PERFORM loop which gets performed until No more records are left in the file. After the READ operation, check the file status code to determine whether the read is successful or Failure.
  6. Final STEP is to CLOSE the file.
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FILER1.
      *
      *    THIS PROGRAM IS TO THE BASIC SEQUENTIAL FILE READ.
      *
      *****************************************************************
      * DATE    USER ID    PROJID               DESCRIPTION
      *05/04/22 MGR003    PRJ123            INITIAL VERISON
      *****************************************************************
      *
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
            SELECT CUSTFILE ASSIGN TO CUSTDD
            ORGANIZATION IS SEQUENTIAL
            ACCESS MODE IS SEQUENTIAL
            FILE STATUS IS WW-FILE-STATUS.
      *
       DATA DIVISION.
       FILE SECTION.
       FD CUSTFILE.
       01 WW-CUST-RECORD       PIC X(050).
      *
       WORKING-STORAGE SECTION.
       01 WW-FILE-STATUS          PIC XX.
       01 WW-NUM1                 PIC 99.
       01 WW-NUM2                 PIC 99.
       01 WW-NUM3                 PIC 999.
       01 WW-NAME                 PIC X(11).
       01 WW-GENDER               PIC X(1).
          88 MALE                  VALUE 'M'.
          88 FEMALE                VALUE 'F'.
          88 GENDER-NOT-PROVIDED   VALUE  ' '.
          88 VALID-GENDER         VALUES 'M' 'F'.
      *
       PROCEDURE DIVISION.
      *
           PERFORM P100-OPEN-FILE
           PERFORM P200-READ-FILE
           PERFORM P300-CLOSE-FILE
      *
           STOP RUN.

       P100-OPEN-FILE.
      *
           OPEN INPUT CUSTFILE
           IF WW-FILE-STATUS NOT = '00'
              DISPLAY 'FILE OPEN FAILED WITH ' WW-FILE-STATUS
              STOP RUN
           END-IF.

       P200-READ-FILE.
      *
           PERFORM UNTIL WW-FILE-STATUS  = '10'
              READ CUSTFILE
              IF WW-FILE-STATUS  = '00'
                 DISPLAY 'CUSTOMER RECORD IS:' WW-CUST-RECORD
              ELSE
                 DISPLAY 'READ FAILED WITH ' WW-FILE-STATUS
              END-IF
           END-PERFORM
           .
       P300-CLOSE-FILE.
      *
           CLOSE CUSTFILE
           IF WW-FILE-STATUS NOT = '00'
              DISPLAY 'FILE CLOSEFAILED WITH ' WW-FILE-STATUS
              STOP RUN
           END-IF.


Add a Comment

Your email address will not be published. Required fields are marked *