COBOL File Section

COBOL Data Division COBOL Working Storage Section

Advertisements


FILE SECTION describes the data is sent to, or comes from computer peripherals specially files.

FILE SECTION describes the fields within the records of each file that was using by the program.

DATA DIVISION, FILE SECTION, FD/SD should start from AREA A and remaining clauses should start in AREA B.


Syntax:

 
DATA DIVISION.
FILE SECTION.
  FD/SD filename 
  [RECORD CONTAINS integer CHARACTERS] 
  [BLOCK CONTAINS integer RECORDS]
  [DATA RECORD IS record-name].
  [RECORDING MODE IS {F/V/U/S}]

FD is a File Description level indicator which will describes the layout of the file defined.

SD is a Sort File Description level indicator which will describes about the sort files.


RECORD CONTAINS Clause


The RECORD CONTAINS clause describes the size, in characters, of data records in a file because each data record of a file is completely defined in a record description entry; this clause is optional for any file description entry.


Syntax:

There are three types of RECORD CONTAINS clauses based on the records length.


  1. Fixed length
  2. Dynamic length
  3. Variable length

1) Fixed length:

If the file is a fixed length file, the declaration for fixed length records will be like below


RECORD CONTAINS integer CHARACTERS

In the fixed length declaration the file record length of the file can be integer.


2) Dynamic length:

If the record length of the file will be provided by the runtime, then syntax will be like below.


RECORD IS VARYING IN SIZE integer1 TO integer2 [DEPENDING ON data-name].

In the dynamic length declaration the file record length of the file can increase from integer1 to integer2 bytes based on the value passed in the data-name which may be passed during the run time.

In this case, the minimum record length would be the value in integer1 and the maximum length would be integer2.


3) Variable length:

If the record length is varying in between fixed lengths then the declaration like below.


RECORD CONTAINS integer1 TO integer2 CHARACTERS.

In the variable length declaration the file record length of the file can increase from integer1 to integer2 bytes.

In this case, the minimum length would be the value in integer1 and the maximum length would be integer2.


BLOCK CONTAINS Clause


The BLOCK CONTAINS clause used to specify the blocking factor of the file being described and if the actual blocking factor of the file being described cannot be determined by the operating system.


Syntax:

There are three types of BLOCK CONTAINS clauses based on the file records length,


  1. Fixed length
  2. Dynamic length
  3. Variable length

1) Fixed length:

If the file is a fixed length file, the declaration for fixed length block will be like below based on the RECORD declaration.


RECORD CONTAINS integer CHARACTERS	
BLOCK CONTAINS integer1 CHARACTERS

In the fixed length declaration the file block length of the file can be integer1.


2) Dynamic length:

If the block length of the file will be provided by the runtime, then syntax will be like below based on the RECORD.


RECORD IS VARYING IN SIZE integer1 TO integer2 [DEPENDING ON data-name].
BLOCK CONTAINS IN SIZE integer3 TO integer4 [DEPENDING ON data-name].

In the dynamic length declaration the file block length of the file can increase from integer3 to integer4 bytes based on the value passed in the data-name which may be passed during the run time.

In this case, the minimum block length would be the value in integer3 and the maximum length would be integer4.


3) Variable length:

If the record length is varying in between fixed lengths based on record length then declaration like below.


RECORD CONTAINS integer1 TO integer2 CHARACTERS.	
BLOCK CONTAINS integer3 TO integer4 CHARACTERS.

In the variable length declaration the file block length of the file can increase from integer3 to integer4 bytes.

In this case, the minimum block length would be the value in integer3 and the maximum length would be integer4.


Note:  BLOCK CONTAINS clause always depends on RECORD CONTAINS clause.


RECORDING MODE Clause:


The RECORDING MODE clause specifies how logical records are contained in the file and also how the logical record being read/written to file.

The REORDING MODE is used for Queue Sequential access mode files.

The RECORD MODE is ignored/not required for VSAM files.

The RECORD MODE clause does not apply to sort-merge file and is optional.


Syntax:

RECORDING MODE IS {F/V/U}

Parameters.

F -


Specifies that file contains fixed length logical records.

BLOCK can contain more than one record.

No OCCURS DEPENDING on clause is associated in record descriptions.

All record is fixed length records and no record length or block descriptor fields required. So no additional memory used to store the record apart from actual record length.

This option is valid for random access and relative files.


V -


Specifies that file contains either fixed length or variable length logical records.

BLOCK can contain more than one record.

For all the variable length records there is record length field is required store the actual length of the variable length record.

The record length field stores at first 4 bytes of each record and not available for user to use it.

Similarly for BLOCK also block descriptor filed required to store the block length.

The block descriptor field stores at first 4 bytes of each block and not available for user to use it.


U -


Specifies that file contains either fixed length or variable length logical records.

One BLOCK contains one record.

Specifies undefined length logical records.

There is no record length and block descriptor fields required.

The BLOCK CONTAINS clause need not be used for this kind of file.


DATA RECORD:


DATA RECORD is used to identify the file record layout to manipulate the data by using the fields in record layout.

DATA RECORD will not have impact on file operation but it just links the record layout to the file.


Practicle Example:


Let’s take an example of reading a Sequential file data and display it through program.

Assume that MTH.EMP.DATA has the employee details.

Let us discuss step by step how the DATA DIVISION can be in the program.


 DATA DIVISION.
 FILE SECTION.
 FD FILE1.
	RECORD CONTAINS 80 CHARACTERS
	BLOCK CONTAINS 800 CHARACTERS
	DATA RECORD IS FILE-REC1
	RECORDING MODE IS F.

 01 FILE1-REC.
	05 EMP-ID	PIC X(05).
	05 EMP-NAME	PIC X(15).
	05 EMP-STATE	PIC X(15).
	05 EMP-COUNTRY	PIC X(10).
	05 FILLER	PIC X(35).

Let's discuss about line by line in the above example.


RECORD CONTAINS 80 CHARACTERS

In the below example, we are using fixed length file of length 80 characters.


BLOCK CONTAINS 800 CHARACTERS

Logical length of block as 800 characters which equals to 10 records length.

In this particular example 1 block contains 10 records.


DATA RECORD IS FILE-REC1

FILE-REC1 is the logical record layout defined for FILE1. So to link the record layout FILE-REC1, it was declared in DATA RECORD clause.


RECORDING MODE IS F.

We are using fixed length file so used the RECORDING MODE as F.


The below proactive example contains actual COBOL program, JCL and file and Output of the program.


Code:



Input:



Jcl:



Output:



Note:  If RECORDING MODE is not specified, an F is the default value.


COBOL Data Division COBOL Working Storage Section

Advertisements