Write data with COBOL.

delmi pittadelmi pitta
3 min read

Table of contents

Hey devs, i am Delmi Pitta from Dominican Republic and i am trying to learn about COBOL and i have to say that this has been a beautiful journey, COBOL is easy to learn, maybe if you have information about better process than this please tell to me.

This program show how we can add indexed information to a file, ‘data.dat’ this is the file type i have used on this COBOL program and this was written on opeCobolIDE from gnuCobol.

To understand the code below please copy and paste on any COBOL editor.

{ IDENTIFICATION DIVISION. PROGRAM-ID. Filing. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. > THIS PART CONNECT US WITH FILES 'DATA.DAT' SELECT EMPLOYEES ASSIGN TO 'DATA.DAT' ORGANIZATION IS INDEXED ACCESS MODE IS DYNAMIC RECORD KEY IS FS-ID. DATA DIVISION. FILE SECTION. > THIS PART MODIFY THE 'DATA.DAT' ABOVE 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). 01 FILE-STATUS PIC XX. 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. IF WS-IF-PROCEDURE = 'W' OR WS-IF-PROCEDURE = 'w' THEN PERFORM UNTIL WS-PERFORM-PROCEDURE = 'N' OPEN I-O EMPLOYEES DISPLAY 'WHAT IS THE EMPLOYEE ID NUMBER?' ACCEPT FS-ID > VerifY IF THE ID ALREADY EXIST OPENING A READ TAG READ EMPLOYEES INVALID KEY > Prompt user for additional information if ID is available DISPLAY 'WHAT IS THE EMPLOYEE NAME?' ACCEPT FS-NAME DISPLAY 'WHAT IS THE EMPLOYEE LAST NAME?' ACCEPT FS-LAST-NAME DISPLAY 'WHAT IS THE EMPLOYEE PHONE NUMBER?' ACCEPT FS-PHONE-NUM DISPLAY 'WHAT IS THE EMPLOYEE EMAIL ADDRESS?' ACCEPT FS-EMAIL DISPLAY 'WHAT IS THE EMPLOYEE SALARY?' ACCEPT FS-SALARY DISPLAY 'WHAT IS THE EMPLOYEE SOCIAL NUMBER?' ACCEPT FS-SOCIAL-NUMBER DISPLAY 'WHAT IS THE EMPLOYEE OCCUPATION?' ACCEPT FS-OCCUPATION DISPLAY 'WHAT IS THE EMPLOYEE SCHEDULE?' ACCEPT FS-SCHEDULE > Write the new record to the file WRITE EMPLOYEES-RECORD > PROTECT THE CURRENT INFORMATION FRON NEWEST INVALID KEY MOVE 'ERROR: RECORD COULD NOT BE WRITTEN' TO WS-ERROR-PROCEDURE DISPLAY WS-ERROR-PROCEDURE NOT INVALID KEY DISPLAY 'Record successfully added!' NOT INVALID KEY > IF THE ID ALREADY EXIST WILL SHOW THIS MOVE 'ERROR: EMPLOYEE ID ALREADY EXISTS' TO WS-ERROR-PROCEDURE DISPLAY WS-ERROR-PROCEDURE > CLOSE THE READ PROCESS END-READ > TO REPEAT THE BUCLE DISPLAY 'WOULD YOU LIKE TO ADD ANOTHER RECORD? (Y/N)' ACCEPT WS-PERFORM-PROCEDURE > IT IS MANDATORY TO CLOSE THE FILE TO CONTINUE WITH PROGRAM CLOSE EMPLOYEES END-PERFORM END-IF. STOP RUN. END PROGRAM Filing.

}

If you execute this program on windows it will show you different question about employees you will be able to fill and save.

And the next program i will show you how to read that saved information on your console.

  • >####Remember this program works on the console or terminal.

  • >#### READ THE COMMENT ARE WITH ‘*>’.

  • >#### PLEASE FOLLOW ME, RESPOND AND SHARE THIS.

0
Subscribe to my newsletter

Read articles from delmi pitta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

delmi pitta
delmi pitta