paint-brush
How to Read Files on COBOLby@delmipitta_0v08l7ko

How to Read Files on COBOL

by Delmi PittaNovember 8th, 2024
Read on Terminal Reader
tldt arrow

Too Long; Didn't Read

Here is a simple tutorial on how to read files on COBOL.
featured image - How to Read Files on COBOL
Delmi Pitta HackerNoon profile picture

Hey hackernooners! I finished a simple COBOL program to read information from a file; it can manage errors

and show the requested information on the console.


Here it is:

IDENTIFICATION DIVISION.
       PROGRAM-ID. Filing.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
        SELECT EMPLOYEES ASSIGN TO 'DATA.DAT'
            ORGANIZATION IS INDEXED
            ACCESS MODE IS DYNAMIC
            RECORD KEY IS FS-ID.

       DATA DIVISION.
       FILE SECTION.
       FD EMPLOYEES.
       01 EMPLOYEES-RECORD.
        05 FS-ID              PIC 9(11).
        05 FS-NAME            PIC X(10).
        05 FS-LAST-NAME       PIC X(10).
        05 FS-PHONE-NUM       PIC 9(11).
        05 FS-SALARY          PIC 9(4).
        05 FS-SOCIAL-NUMBER   PIC 9(11).
        05 FS-OCCUPATION      PIC X(30).
        05 FS-SCHEDULE        PIC X(30).
        05 FS-EMAIL           PIC X(30).

       WORKING-STORAGE SECTION.
       01 WS-IF-PROCEDURE         PIC X(1).
       01 WS-PERFORM-PROCEDURE     PIC X(1).
       01 WS-ERROR-PROCEDURE       PIC X(50).
   

       PROCEDURE DIVISION.

       BEGIN.
       DISPLAY 'PLEASE CHOOSE AN OPTION BELOW: WRITE(W) READ(R)'.
       DISPLAY 'WHAT DO YOU WANT TO DO? DELETE(D) MODIFY(M)'.
       ACCEPT WS-IF-PROCEDURE.
        
      *>   BE CAREFUL WHEN YOU ASK A USER TO WRITE 'LOWERCASE AND UPPERCASE'
           IF WS-IF-PROCEDURE = 'R' OR WS-IF-PROCEDURE = 'r' THEN
           
           
      *>      THIS BUCLE WILL REPEAT UNTIL THE USER WRITE 'N'
           PERFORM UNTIL WS-PERFORM-PROCEDURE = 'N'
        
             *>   WE OPEN THE FILE WITH OPEN I-O
           OPEN I-O EMPLOYEES
            *>   WE WIRTE THE MAIN KEY TO FIND IT
            DISPLAY 'WHAT IS THE EMPLOYEE ID NUMBER?'
            ACCEPT FS-ID
           
           
      *>       WE READ THE FILE
           READ EMPLOYEES
               KEY IS fs-id
               INVALID KEY
      *>      IF THERE IS NOT A FILE ASOCIATED WITH THE ID WE CAN SHOW THIS
               DISPLAY 'WRONG ID OR USER DOES NOT EXIST, TRY IT AGAIN'
                   MOVE 'y' TO WS-PERFORM-PROCEDURE
                   
      *>              IF THE FILE EXIST WE CAN PROCEDE READING
               NOT INVALID KEY
      *>          HERE WE WRITE EVERYTHIN WE WANT TO DISPLAY ON THE CONSOLE
                   DISPLAY 'RECORD FOUND: '
                   DISPLAY 'ID:   ' FS-ID
                   DISPLAY 'NAME: ' FS-NAME
                   DISPLAY 'LAST NAME:      ' FS-LAST-NAME
                   DISPLAY 'SOCIAL NUMBER:  ' FS-SOCIAL-NUMBER
                   
                   DISPLAY'----COMMUNICATION ZONE----'
                   DISPLAY'EMPLOYEE PHONE NUMBER: ' FS-PHONE-NUM
                   DISPLAY'EMAIL ADDRESS:         ' FS-EMAIL
                   
                   DISPLAY'---EMPLOEE PAYMENT INFORMATION---'
                   DISPLAY'OCCUPATION: ' FS-OCCUPATION
                   DISPLAY'SALARY :    ' FS-SALARY
                   
                   DISPLAY'---EMPLOEE SCHEDULE---'
                   DISPLAY'SCHEDULE    ' FS-SCHEDULE
                   
           END-READ
                  
           
            *>      IT IS MANDATORY TO CLOSE THE FILE TO CONTINUE WITH PROGRAM
            CLOSE EMPLOYEES
            
      *>       HERE WE CAN REPEAT THE BUCLE.
      *>       IF WE SELECT Y THE PROCCESS START AGAIN
      *>       BUT IF WE SELECT N THE PROCCESS STOP
            DISPLAY'WOULD YOU LIKE TO READ ANOTHER FILE? Y/N'
            ACCEPT WS-PERFORM-PROCEDURE
            
             END-PERFORM
             END-IF.

              STOP RUN.

            END PROGRAM Filing.

This is the main code you can execute on your openCobolIDE or gnuCobol. If there is an error, the system will show you this information below.


From my PC and OpenCobolIDE.