Open In App

File Section in COBOL

Last Updated : 30 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 application deployment on mainframe computers.

File Selection

In COBOL, file selection is the process of identifying which records in a file should be processed or read. This is typically done using the SELECT and ASSIGN clauses in the FILE-CONTROL paragraph of the COBOL program.

The SELECT clause is used to specify the file that will be used in the program, and the ASSIGN clause is used to specify the device or file name where the file is stored.

For example, to select a file called “CUSTOMER” that is stored on a magnetic tape, the following code could be used:

Cobol




SELECT CUSTOMER-FILE 
ASSIGN TO TAPE


Additionally, the COBOL program can use the READ statement and a conditional statement to read only the records that meet certain conditions and skip the rest of the records.

For example, to read only the records of customers with a balance greater than zero:

Cobol




READ CUSTOMER-FILE
INTO CUSTOMER-RECORD
IF CUSTOMER-RECORD-BALANCE > 0
PERFORM PROCESS-CUSTOMER


Note: The file selection and reading methods may vary depending on the specific COBOL implementation and platform being used.

Record Contains Clauses 

The RECORD CONTAINS clause is used in the FILE SECTION of the COBOL program to specify the record layout of a file. It is used to define the size and position of each data field within a record, as well as the type of record format that is used.

Syntax:

RECORD CONTAINS integer [TO integer] [BYTE] [CHARACTER]

Where “integer” is the number of bytes or characters in the record, and “TO integer” is used to specify the maximum number of bytes or characters. The optional “BYTE” or “CHARACTER” keyword is used to specify whether the record is in byte or character format.

Here’s an example of how the RECORD CONTAINS clause could be used in a COBOL program:

Cobol




FD CUSTOMER-FILE
RECORD CONTAINS 80 CHARACTERS
DATA RECORD IS CUSTOMER-RECORD.


This example defines a file called “CUSTOMER-FILE” with a record format that contains 80 characters. The record layout is defined in the “CUSTOMER-RECORD” data record.

There are three main types of record format that can be specified using the RECORD CONTAINS clause:

1. Fixed-length records:

 The record length is fixed and does not change. For example, if a record contains 10 bytes of data, it will always be 10 bytes long, regardless of the amount of data stored in the record.

Syntax:

 RECORD CONTAINS 10 BYTE

2. Dynamic-length records: 

The record length can change, but the maximum length is defined. For example, a record can contain up to 10 bytes of data, but it may only contain 5 bytes if that is all the data that is stored in the record. 

Syntax:

 RECORD CONTAINS 1 TO 10 BYTE

3. Variable-length records:

 The record length can change, and there is no maximum length defined. For example, a record can contain any number of bytes of data. This format is often used for variable-length fields such as text or variable-length elements. 

Syntax:

RECORD CONTAINS VARYING BYTE

BLOCK CONTAINS Clauses

The BLOCK CONTAINS clause is used in the FILE SECTION of the COBOL program to specify the block size of a file. It is used to define the number of records that can be stored in a block, as well as the type of block format that is used.

Syntax:

BLOCK CONTAINS integer [RECORD] [TO integer] [RECORD]

where “integer” is the number of records in the block and “TO integer” is used to specify the maximum number of records. The optional “RECORD” keyword is used to specify that the block size is in terms of records.

Here’s an example of how the BLOCK CONTAINS clause could be used in a COBOL program:

Cobol




FD CUSTOMER-FILE
BLOCK CONTAINS 10 RECORD
RECORD CONTAINS 80 CHARACTERS
DATA RECORD IS CUSTOMER-RECORD.


This example defines a file called “CUSTOMER-FILE” with a block format that contains 10 records. Each record contains 80 characters and the record layout is defined in the “CUSTOMER-RECORD” data record.

Note: The BLOCK CONTAINS clause is used in conjunction with the RECORD CONTAINS clause, the BLOCK CONTAINS defines how many records are in a block and the RECORD CONTAINS defines the layout of each record.

There are three main types of block format that can be specified using the BLOCK CONTAINS clause:

1. Fixed-length blocks:

The block size is fixed and does not change. For example, if a block contains 10 records, it will always be 10 records long, regardless of the amount of data stored in the block.

Syntax:

 BLOCK CONTAINS 10 RECORD

2. Dynamic-length blocks:

The block size can change, but the maximum size is defined. For example, a block can contain up to 10 records, but it may only contain 5 records if that is all the data that is stored in the block.

Syntax:

BLOCK CONTAINS 1 TO 10 RECORD

3.Variable-length blocks: 

The block size can change, and there is no maximum size defined. For example, a block can contain any number of records. This format is often used for variable-length fields such as text or variable-length elements. 

Syntax: 

BLOCK CONTAINS VARYING RECORD

RECORDING MODE Clause 

The RECORDING MODE clause and the BLOCK CONTAINS clause are used in the FILE-CONTROL paragraph of the COBOL program to specify the record and block format of a file.

The RECORDING MODE clause is used to specify the type of record format that is used in a file. 

Syntax:

 RECORDING MODE IS {F | V | U}

Where “F” stands for Fixed, “V” stands for Variable and “U” stands for undefined.

  • Fixed-length records: The record length is fixed and does not change.
  • Variable-length records: The record length can change, and there is no maximum length defined.
  • Undefined-length records: the record length is not defined.

BLOCK Clause :

The BLOCK CONTAINS clause is used to specify the block size of a file.

Syntax:

BLOCK CONTAINS integer [RECORD] [TO integer] [RECORD]

where “integer” is the number of records in the block and “TO integer” is used to specify the maximum number of records. The optional “RECORD” keyword is used to specify that the block size is in terms of records.

DATA RECORD:

The DATA RECORD is a Data Division item that describes the layout of the records in a file.

 Syntax:

   DATA RECORD IS record-name.

Where “record-name” is the name of the record in the Data Division that contains the layout of the records in the file.

Here’s an example of how the RECORDING MODE, BLOCK CONTAINS, and DATA RECORD clauses could be used in a COBOL program:

Cobol




FD CUSTOMER-FILE
    RECORDING MODE IS F
    BLOCK CONTAINS 10 RECORD
    DATA RECORD IS CUSTOMER-RECORD.


This example defines a file called “CUSTOMER-FILE” with a fixed-length record format, a block format that contains 10 records and the layout of the records is defined in the “CUSTOMER-RECORD” Data Division item

It’s worth noting that the use of these clauses may vary depending on the specific COBOL implementation and platform being used.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads