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:
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…”.
- In this example, The PROGRAM-ID header specifies the name of the program, which is “MYPROGRAM”.
- The ENVIRONMENT DIVISION contains the INPUT-OUTPUT SECTION, which is used to define the input and output files used by the program. The SELECT statement is used to specify the names of the files, and the ASSIGN clause is used to specify the location of the files on the computer. In this case, the files are located in the “/uploads” directory.
- The DATA DIVISION contains the FILE SECTION, which is used to define the structure of the records in the input files. The FD (File Description) entry is used to define the file and the record layout for the file. The 01-level items that follow the FD entry define the individual records in the file. In this case, there are two input files, “FILE-1” and “FILE-2”, each with a single record containing two fields.
- The WORKING-STORAGE SECTION is used to define working storage variables that can be used to store intermediate values during the execution of the program. In this case, there are four working storage variables: “WS-FILE-1”, “WS-EOF-1”, “WS-FILE-2”, and “WS-EOF-2”.
- The PROCEDURE DIVISION contains the main logic of the program. The OPEN statement is used to open “FILE-1” for input, and the PERFORM UNTIL loop reads and displays the contents of the file until the end of the file is reached. The AT END clause is used to set the “WS-EOF-1” variable to ‘Y’ when the end of the file is reached, and the “WS-EOF-1” variable is used as the loop condition.
- The CLOSE statement is used to close “FILE-1” after all the records have been read. The same process is then repeated for “FILE-2”.
- Finally, the STOP RUN statement is used to stop the program execution.
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:
- 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).
- ASSIGN: This clause is used to specify the device where the file is stored (e.g., DISK, TAPE).
- ORGANIZATION: This clause is used to specify the organization of the file (e.g., SEQUENTIAL, RELATIVE).
- ACCESS MODE: This clause is used to specify the mode in which the file will be accessed (e.g., SEQUENTIAL, RANDOM).
- RECORDING MODE: This clause is used to specifies the record format for the file (e.g., fixed-length, variable-length).
- RECORD KEY: This clause is used to specify the key used to access records in the file (e.g., PRIMARY KEY, ALTERNATE KEY).
- RECORD DELIMITER: This clause is used to specify the character that separates records in the file (e.g., NEWLINE).
- RECORD TYPE: This clause is used to specify the type of records in the file (e.g., VARIABLE, FIXED).
- 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 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 TO physical-file-name
ORGANIZATION IS organization-type
ACCESS MODE IS access-mode
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:
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.
Please Login to comment...