Show List

Input and Output Operations

Input and output operations are an essential part of most COBOL programs. COBOL provides several ways to input and output data, including reading and writing data to files, reading and writing data from the console, and using standard input/output procedures. Let's look at each of these methods in more detail.

Console Input and Output: The simplest way to input and output data in a COBOL program is to use standard input/output procedures. The following example reads a value from the console and displays it on the screen:

java
Copy code
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME PIC X(20). PROCEDURE DIVISION. DISPLAY "What is your name?". ACCEPT NAME. DISPLAY "Hello, " NAME "!". STOP RUN.

In this example, the ACCEPT statement reads a value from the console and stores it in the variable NAME, and the DISPLAY statement outputs a message to the console.

File Handling: COBOL also provides robust support for file handling, which allows programs to read and write data to and from files. The following example reads data from a file and outputs it to the console:

vbnet
Copy code
IDENTIFICATION DIVISION. PROGRAM-ID. READ-FILE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT SALES-FILE ASSIGN TO "SALES.DAT". DATA DIVISION. FILE SECTION. FD SALES-FILE. 01 SALES-RECORD. 05 DATE PIC X(8). 05 ITEM-NO PIC X(10). 05 AMOUNT PIC 9(7)V99. WORKING-STORAGE SECTION. 01 TOTAL-AMOUNT PIC 9(9)V99 VALUE 0. PROCEDURE DIVISION. OPEN INPUT SALES-FILE. READ SALES-FILE INTO SALES-RECORD AT END GO TO END-OF-FILE. DISPLAY "Sales for " DATE ": " ITEM-NO " - " AMOUNT. ADD AMOUNT TO TOTAL-AMOUNT. PERFORM UNTIL END-OF-FILE READ SALES-FILE INTO SALES-RECORD AT END GO TO END-OF-FILE. DISPLAY "Sales for " DATE ": " ITEM-NO " - " AMOUNT. ADD AMOUNT TO TOTAL-AMOUNT. END-PERFORM. DISPLAY "Total sales: " TOTAL-AMOUNT. CLOSE SALES-FILE. STOP RUN. END-OF-FILE. DISPLAY "End of file.". STOP RUN.

In this example, the program reads data from a file named SALES.DAT, and displays the data on the console. The program also calculates the total sales amount and displays it at the end.

In conclusion, COBOL provides several ways to input and output data in programs, and the choice of method depends on the specific needs of the program. File handling is one of the most powerful features of COBOL, as it allows programs to store and manipulate large amounts of data.


    Leave a Comment


  • captcha text