Open In App

Input-Output Section in COBOL

In COBOL, the INPUT-OUTPUT SECTION is a section of the program where you can define file-handling statements. These statements are used to read from or write to external files, such as data files or report files.

The INPUT-OUTPUT SECTION follows the FILE SECTION and comes before the PROCEDURE DIVISION in a COBOL program. It is optional, and you only need to include it if you are using file handling statements in your program.



Here is an example of the INPUT-OUTPUT SECTION in a COBOL program:

Example 1: 






PROGRAM-ID. MYPROGRAM.
  
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE-1
                ASSIGN TO "/uploads/FILE-1.txt".
            SELECT FILE-2
                ASSIGN TO "/uploads/FILE-2.txt".
    DATA DIVISION.
        FILE SECTION.
        FD FILE-1.
        01 FILE-1-RECORD.
            05 FILE-1-FIELD1 PIC X(9).
            05 FILE-1-FIELD2 PIC A(10).
        FD FILE-2.
        01 FILE-2-RECORD.
            05 FILE-2-FIELD1 PIC X(10).
            05 FILE-2-FIELD2 PIC A(10).
          
        WORKING-STORAGE SECTION.
        01 WS-FILE-1 PIC X(9).
        01 WS-EOF-1 PIC A(1).
        01 WS-FILE-2 PIC X(10).
        01 WS-EOF-2 PIC A(1).
          
    PROCEDURE DIVISION.
        OPEN INPUT FILE-1.
            PERFORM UNTIL WS-EOF-1='Y'
                READ FILE-1 INTO WS-FILE-1
                    AT END MOVE 'Y' TO WS-EOF-1
                    DISPLAY FILE-1-FIELD1 FILE-1-FIELD2
                END-READ
            END-PERFORM.
        CLOSE FILE-1.
        OPEN INPUT FILE-2.
            PERFORM UNTIL WS-EOF-2='Y'
                READ FILE-2 INTO WS-FILE-2
                    AT END MOVE 'Y' TO WS-EOF-2
                    DISPLAY FILE-2-FIELD1 FILE-2-FIELD2
                END-READ
            END-PERFORM.
        CLOSE FILE-2.
    STOP RUN.
  
*> This code is contributed by Susobhan Akhuli

Output:

GFG...! Welcome to 
Akhuli...f Susobhan

This COBOL program reads and displays the contents of two input files, “FILE-1.txt” and “FILE-2.txt”, located in the “/uploads” directory. If “FILE-1.txt” contains the text “Hello!! Welcome to GFG…” and “FILE-2.txt” contains the text “Hi. Myself Susobhan Akhuli…”.

Explanation:

Note: This is just a simple example to illustrate the structure of the INPUT-OUTPUT SECTION. In a real COBOL program, you may have more complex file-handling logic, depending on your requirements.

The FILE-CONTROL system in COBOL is used to define external files that are accessed by the program. It consists of several clauses that specify the attributes of the files, such as the file name, the device where the file is stored, the record format, and the access mode.

Here are some of the clauses that can be used in the FILE-CONTROL system:

  1. SELECT: This clause is used to specify the name and attributes of the file. It includes the file name and the device where the file is stored (e.g., FILE-1, FILE-2).
  2. ASSIGN: This clause is used to specify the device where the file is stored (e.g., DISK, TAPE).
  3. ORGANIZATION: This clause is used to specify the organization of the file (e.g., SEQUENTIAL, RELATIVE).
  4. ACCESS MODE: This clause is used to specify the mode in which the file will be accessed (e.g., SEQUENTIAL, RANDOM).
  5. RECORDING MODE: This clause is used to specifies the record format for the file (e.g., fixed-length, variable-length).
  6. RECORD KEY: This clause is used to specify the key used to access records in the file (e.g., PRIMARY KEY, ALTERNATE KEY).
  7. RECORD DELIMITER: This clause is used to specify the character that separates records in the file (e.g., NEWLINE).
  8. RECORD TYPE: This clause is used to specify the type of records in the file (e.g., VARIABLE, FIXED).
  9. FILE STATUS: This clause is used to specifies the name of a data item that will be used to store the status of file operations (e.g., INPUT-STATUS, OUTPUT-STATUS).

Below is the syntax for the clauses that can be used in the FILE-CONTROL system in COBOL:

Select clause:

SELECT logical-file-name
    [ASSIGN TO physical-file-name]
    [ORGANIZATION IS organization-type]
    [ACCESS MODE IS access-mode]
    [RECORDING MODE IS record-mode]
    [RECORD KEY IS record-key]
    [RECORD DELIMITER IS record-delimiter]
    [RECORD TYPE IS record-type]
    [FILE STATUS IS file-status]

ASSIGN clause: 

ASSIGN TO physical-file-name

ORGANIZATION clause:

ORGANIZATION IS organization-type

ACCESS clause:

ACCESS MODE IS access-mode

RECORDING clause:

RECORDING MODE IS record-mode

RECORD KEY clause:

RECORD KEY IS record-key

RECORD DELIMITER clause:

RECORD DELIMITER IS record-delimiter

RECORD TYPE clause:

RECORD TYPE IS record-type

FILE STATUS clause:

FILE STATUS IS file-status

Below is an example of the FILE-CONTROL system with all the above clauses:

Example 2: 




FILE-CONTROL.
    SELECT FILE-1
        ASSIGN TO "C:\data\input.txt"
        ORGANIZATION IS SEQUENTIAL
        ACCESS MODE IS SEQUENTIAL
        RECORDING MODE IS VARIABLE
        RECORD KEY IS INPUT-KEY
        RECORD DELIMITER IS ','
        RECORD TYPE IS INPUT-TYPE
        FILE STATUS IS INPUT-STATUS.
    SELECT FILE-2
        ASSIGN TO "C:\data\output.txt"
        ORGANIZATION IS SEQUENTIAL
        ACCESS MODE IS SEQUENTIAL
        RECORDING MODE IS VARIABLE
        RECORD KEY IS OUTPUT-KEY
        RECORD DELIMITER IS ','
        RECORD TYPE IS OUTPUT-TYPE
        FILE STATUS IS OUTPUT-STATUS.

Note: This is just a simple example to illustrate the syntax of the FILE-CONTROL system. In a real COBOL program, you may have more complex file-handling logic, depending on your requirements.

In this example, the FILE-CONTROL system defines a file named FILE-1 that is stored on the “”C:\data” directory. The file is organized sequentially, and it can be accessed sequentially. The records in the file are delimited by a newline character and are of variable length. The primary key is used to access the records in the file.


Article Tags :