Open In App

File Declaration in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

COBOL (Common Business-Oriented Language) is a high-level programming language that was developed in the 1950s for business applications. In COBOL, a file is a collection of records that are stored on external storage devices such as tapes or disks. A file can be input, output, or both input and output.

To declare a file in COBOL, you need to use the FILE SECTION and FD (File Description) clauses. The FD clause describes the structure and attributes of the file, such as the type of record format (fixed-length, variable-length, or undefined), the blocking factor, and the data structure that describes the records in the file. You can also use the SELECT clause in the FD statement to allocate the file to a specific device and the FILE STATUS clause to specify the name of the file status variable used to check the status of file operations.

COBOL provides several options for handling files, such as ORGANIZATION, ACCESS MODE, FILE TYPE, and RESERVE, which can be used to specify the organization and access mode of the file, the type of file (sequential, indexed, etc.), and the number of file records to be reserved. You can use file manipulation verbs such as OPEN, CLOSE, READ, WRITE, and REWRITE to perform input/output operations on files. COBOL also provides options for accessing individual records in a file, such as RECORD KEY, ALTERNATE RECORD KEY, and RELATIVE KEY, which can be used to access records using a key field or a relative record number.

Note: The path of the text file named “text.txt” must be “/uploads/text.txt” otherwise you have to change the path in your system.

The text.txt file contains:

Hi. Myself Susobhan Akhuli.
Hello! Geeks...
Welcome To GFG!!

Here is an example of how to declare a file in COBOL:

Example 1:

Cobol




FILE SECTION.
FD MyFile
    RECORDING MODE IS F
    BLOCK CONTAINS 0 RECORDS
    DATA RECORD IS MyRecord.
01 MyRecord.
    02 Field1 PIC X(20).
    02 Field2 PIC X(10).
    02 Field3 PIC X(30).


In this example, MyFile is the name of the file and MyRecord is the name of the record in the file. The FD clause specifies that the file has fixed-length records and that each record is described by the MyRecord data structure. The BLOCK CONTAINS 0 RECORDS clause specifies that the file does not have a blocking factor, which means that records are not grouped into blocks on the external storage device.

Below is the implementation of the above example:

Cobol




PROGRAM-ID. MYPROGRAM.
  
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE-1
                ASSIGN TO "/uploads/text.txt".
    DATA DIVISION.
        FILE SECTION.
        FD FILE-1
            RECORDING MODE IS F
            BLOCK CONTAINS 0 RECORDS
            DATA RECORD IS FILE-1-RECORD.
        01 FILE-1-RECORD.
            02 FILE-1-Field1 PIC X(20).
            02 FILE-1-Field2 PIC X(10).
            02 FILE-1-Field3 PIC X(30).
          
        WORKING-STORAGE SECTION.
        01 WS-FILE-1 PIC X(9).
        01 WS-EOF-1 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 FILE-1-Field3
                END-READ
            END-PERFORM.
        CLOSE FILE-1.
    STOP RUN.


Output:

!!
Myself Susobhan Akhuli.
Hello! Geeks...
Welcome To GFG

Here is another example of how to declare a file in COBOL:

Example 2:

Cobol




FILE SECTION.
   FD MyFile
       RECORDING MODE IS F
       BLOCK CONTAINS 10 RECORDS
       DATA RECORD IS MyRecord.
   01 MyRecord.
       02 Field1 PIC X(20).
       02 Field2 PIC X(10).
       02 Field3 PIC X(30).


This code defines a file named MyFile with fixed-length records, a blocking factor of 10, and a data structure named MyRecord. The MyRecord data structure describes the records in the file and consists of three fields: Field1, Field2, and Field3.

Below is the implementation of the above example:

Cobol




PROGRAM-ID. MYPROGRAM.
  
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE-1
                ASSIGN TO "/uploads/text.txt".
    DATA DIVISION.
        FILE SECTION.
        FD FILE-1
            RECORDING MODE IS F
            BLOCK CONTAINS 10 RECORDS
            DATA RECORD IS FILE-1-RECORD.
        01 FILE-1-RECORD.
            02 FILE-1-Field1 PIC X(20).
            02 FILE-1-Field2 PIC X(10).
            02 FILE-1-Field3 PIC X(30).
          
        WORKING-STORAGE SECTION.
        01 WS-FILE-1 PIC X(9).
        01 WS-EOF-1 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 FILE-1-Field3
                END-READ
            END-PERFORM.
        CLOSE FILE-1.
    STOP RUN.


Output:

!!
Myself Susobhan Akhuli.
Hello! Geeks...
Welcome To GFG

There are several types of file declarations in COBOL, based on the type of access required for the file and the organization of the records in the file.

  1. Sequential file: A sequential file is a file in which records are stored in sequential order. To declare a sequential file, you need to use the SEQUENTIAL clause in the FD statement.
  2. Indexed file: An indexed file is a file in which records are stored in a non-sequential order and are accessed using an index. To declare an indexed file, you need to use the INDEXED clause in the FD statement.
  3. Relative file: A relative file is a file in which records are stored in a non-sequential order and are accessed using a relative record number. To declare a relative file, you need to use the RELATIVE clause in the FD statement.
  4. External file: An external file is a file that is stored on an external storage device, such as a disk or tape. To declare an external file, you need to use the EXTERNAL clause in the FD statement.
  5. Input file: An input file is a file that is used to input data into the COBOL program. To declare an input file, you need to use the INPUT clause in the FD statement.
  6. Output file: An output file is a file that is used to output data from the COBOL program. To declare an output file, you need to use the OUTPUT clause in the FD statement.
  7. Input-output file: An input-output file is a file that is used for both input and output operations in the COBOL program. To declare an input-output file, you need to use the INPUT-OUTPUT clause in the FD statement.

File Allocation: 

File allocation is the process of assigning a file to a specific input/output device, such as a disk or tape. In COBOL, you can use the SELECT clause in the FD statement to allocate a file to a specific device.

Here is the syntax for the SELECT clause:

SELECT FileName [ASSIGN TO DeviceName]

The fileName is the name of the file being allocated and DeviceName is the name of the device to which the file is being allocated.

File Definition: 

File definition is the process of describing the structure and attributes of a file in COBOL. You can use the FD (File Description) clause in the FILE SECTION to define a file.

Below is the syntax for the FD clause:

Cobol




FD FileName
   [RECORDING MODE {F | V | U}]
   [BLOCK CONTAINS n RECORDS]
   [DATA RECORD IS DataName]
   [LABEL RECORD IS {STANDARD | DataName}]
   [FILE STATUS IS FileStatus]


The fileName is the name of the file being defined. The RECORDING MODE clause specifies the type of record format used in the file (fixed-length, variable-length, or undefined). The BLOCK CONTAINS clause specifies the number of records per block in the file. The DATA RECORD clause specifies the name of the data structure that describes the records in the file. The LABEL RECORD clause specifies the type of label record used in the file (standard or user-defined). The FILE STATUS clause specifies the name of the file status variable used to check the status of file operations.

Below is an example of how to define a file named MyFile with fixed-length records, a blocking factor of 10, and a data structure named MyRecord:

Example 3:

Cobol




PROGRAM-ID. MYPROGRAM.
  
ENVIRONMENT DIVISION.
    INPUT-OUTPUT SECTION.
        FILE-CONTROL.
            SELECT FILE-1
                ASSIGN TO "/uploads/text.txt"
                STATUS IS FileStatus.
    DATA DIVISION.
        FILE SECTION.
        FD FILE-1
            RECORDING MODE IS F
            BLOCK CONTAINS 10 RECORDS
            DATA RECORD IS FILE-1-RECORD
            LABEL RECORD IS STANDARD.
              
        01 FILE-1-RECORD.
            02 FILE-1-Field1 PIC X(20).
            02 FILE-1-Field2 PIC X(10).
            02 FILE-1-Field3 PIC X(30).
          
        WORKING-STORAGE SECTION.
        01 WS-FILE-1 PIC X(9).
        01 WS-EOF-1 PIC A(1).
        01 FileStatus PIC X(2).
          
    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 FILE-1-Field3
                END-READ
            END-PERFORM.
        CLOSE FILE-1.
    STOP RUN.


Output:

!!
Myself Susobhan Akhuli.
Hello! Geeks...
Welcome To GFG

Here are some additional subtopics and different ways that are related to the concept of file declarations in COBOL:

  • File manipulation verbs: In COBOL, you can use file manipulation verbs such as OPEN, CLOSE, READ, WRITE, and REWRITE to perform input/output operations on files.
  • File handling options: COBOL provides several options for handling files, such as ORGANIZATION, ACCESS MODE, FILE TYPE, and RESERVE, which can be used to specify the organization and access mode of the file, the type of file (sequential, indexed, etc.), and the number of file records to be reserved.
  • File status codes: COBOL uses file status codes to indicate the status of file operations. These codes are returned in the file status variable specified in the FD statement and can be used to check for errors or to determine the end-of-file condition.
  • Record-level addressing: COBOL provides several options for accessing individual records in a file, such as RECORD KEY, ALTERNATE RECORD KEY, and RELATIVE KEY, which can be used to access records using a key field or a relative record number.
  • Sequential file processing: In COBOL, you can use the SEQUENTIAL clause in the FD statement to declare a sequential file and perform input/output operations on the file using the READ and WRITE verbs.
  • Indexed file processing: In COBOL, you can use the INDEXED clause in the FD statement to declare an indexed file and access records using an index. You can use the START and READ verbs to search for and retrieve records from the file.
  • Relative file processing: In COBOL, you can use the RELATIVE clause in the FD statement to declare a relative file and access records using a relative record number. You can use the START and READ verbs to search for and retrieve records from the file.
  • External file processing: In COBOL, you can use the EXTERNAL clause in the FD statement to declare an external file and perform input/output operations on the file using the OPEN, CLOSE, READ, and WRITE verbs.


Last Updated : 28 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads