File Declaration in COBOL
Last Updated :
24 Apr, 2025
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Similar Reads
Data Item Declaration in COBOL
Data Item declaration is nothing but declaring the variables used in a COBOL program. To declare the variables in a program, we should start with the level number and name of the variable. There are some optional clauses that we can declare after the name of the variable such as PICTURE, VALUE, DISP
5 min read
Display Computation in COBOL
DISPLAY is the most common form of internal data representation. DISPLAY stores in decimal form. Each character of the data will represent one byte of storage. If there is no usage clause for data items, then by default it will come under DISPLAY. DISPLAY can be used for all types namely Numeric da
2 min read
String Handling in COBOL
The string is the data type, used to represent the sequence of characters, which ends with /0. String provides much functionality which makes our programming easy when it comes to groups of chars, words, sentences, or lines. String Handling:  String handling is the process or method to handle the s
7 min read
Working Storage Section in COBOL
Cobol is a high-level language, which has its own compiler. The COBOL compiler translates the COBOL program into an object program, which is finally executed. To execute the COBOL program without any error, these divisions must be written in the order in which they are specified below: 1. Identific
3 min read
Programming Construction in COBOL
COBOL is a compiled, imperative, procedural programming language designed for business use. It can handle very large numbers and strings. COBOL is still in use today because it's robust, with an established code base supporting sprawling enterprise-wide programs. Learning to program in COBOL will se
5 min read
Data Layout in COBOL
COBOL is a programming language that was developed in the 1950s for business and financial applications. In COBOL, the data layout is the arrangement of data items in a program. It specifies how the data is organized and how it is accessed. COBOL programs are organized into four divisions: the iden
8 min read
File Section in COBOL
COBOL is a high-level programming language for business applications or business use. It was designed in 1959 by the Conference on Data Systems Language (CODASYL). It was primarily used in business, finance, and administration system for companies and governments. COBOL is still widely used in appli
6 min read
Linkage Section in COBOL
The linkage Section in COBOL is used to declare the variables and it is used in calling the variable from another program. It is used in calling the variable or data from the called program by using the CALL statement. The linkage Section helps in sending the data to the calling program or receivin
4 min read
Variables in COBOL
Declaring and initializing variables might be simple for programmers. However, if this is your first programming language, think of a variable as a container in which you can fill an unknown quantity â a value of said data type. Based on its use, this variable's value is then stored at a particular
8 min read
Data Type Justification in COBOL
Data Classification/Types helps the system identify how the programmer wants to use the data in any particular program. In COBOL we have three different Classifications of DATA TYPES, which are Numeric, Alphabetic, and Alpha-Numeric data types. In COBOL DATA TYPE justification applies to Numeric dat
4 min read