COBOL Sequential and Relative File
Sequential file handling in COBOL
- Sequential file is also known as a flat-file.
- It stores records in a sequential manner, i.e., one after another, stacked.
- In this, we cannot directly access to the Nth record. For this, we have to read the records till N-1th before proceeding to the Nth record.
- All the new records are added to the end, i.e., first come, first serve.
- These files can have either fixed or variable length.
- If there is a case of simple read and write a function and very little search of the record is required, these files are recommended.
SYNTAX =>
1
2
3
4
5
6
|
SELECT logical – fl ASSIGNED to physical – fl
ORGANIZATION IS SEQUENTIAL
SELECT logical – fl ASSIGNED to physical – fl
ORGANIZATION IS LINE SEQUENTIAL
|
These must be coded in the FILE – CONTROL SECTION under INPUT-OUTPUT SECTION, defined under ENVIRONMENTAL DIVISION.
SAMPLE PROGRAM -1
This is a demo program to learn and understand the concept of sequential file handling and how to assign values from a different subprogram.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
IDENTIFICATION DIVISION.
PROGRAM–ID. SEQFILE.
ENVIRONMENT DIVISION.
INPUT–OUTPUT SECTION.
FILE–CONTROL.
*INPYUT FILE
SELECT INFL ASSIGN TO INFLDD
ORGANIZATION IS SEQUENTIAL
FILE STATUS FS–INFL.
DATA DIVISION.
FILE SECTION.
FD INFL.
01 INFL–REC.
05 STORE–ID PIC 9(05).
05 FILLER PIC X(01).
05 ITEM–ID PIC X(10).
05 FILLER PIC X(64).
WORKING–STORAGE SECTION.
01 FS–INFL PIC X(02) VALUE SPACES.
88 FS–INFL–OK VALUE ’00’.
88 FS–INFL–EOF VALUE ’10’.
01 COUNTERS.
05 READ–COUNT PIC 9(2).
05 WRITE–COUNT PIC 9(2).
PROCEDURE DIVISION.
MAIN–PARA.
PERFORM OPEN–PARA THRU OPEN–EXIT–PARA.
PERFORM PROCESS–PARA THRU PROCESS–EXIT–PARA.
PERFORM CLOSE–PARA THRU CLOSE–EXIT–PARA.
STOP RUN.
OPEN–PARA.
INITIALIZE FS–INFL READ–COUNT WRITE–COUNT.
OPEN INPUT INFL
IF FS–INFL–OK
CONTINUE
ELSE
DISPLAY “FILE OPEN FAILED: “ FS–INFL
END–IF.
OPEN–EXIT–PARA.
EXIT.
PROCESS–PARA.
PERFORM UNTIL FS–INFL–EOF.
READ INFL
AT END
IF READ–COUNT < 1.
DISPLAY ‘NO RECORDS PRESENT’
GO TO EXIT–PARA
END–IF
NOT AT END
PERFORM WRITE–PARA THRU WRITE–EXIT–PARA
END–READ
END–PERFORM.
PROCESS–EXIT–PARA.
EXIT.
WRITE–PARA.
ADD 1 TO READ–COUNT.
IF STORE–ID > 12346
DISPLAY ‘STORE-ID: ‘ STORE–ID
DISPLAY ‘ITEM-ID: ‘ ITEM–ID
END–IF.
WRITE–EXIT–PARA.
EXIT.
CLOSE–PARA.
CLOSE INFL.
CLOSE–EXIT–PARA.
EXIT.
EXIT–PARA.
EXIT PROGRAM.
|
THE OUTPUT WILL BE
1
2
3
4
5
6
7
8
|
STORE ID: 12347
ITEM ID: DETER12347
STORE ID: 12348
ITEM ID: JKLH67545
STORE ID: 12350
ITEM ID: KLJ443232
|
Here in this program, we have assigned a few variables with different values and later manipulated the output using different data file handling properties to clarify the different concepts related to CALLED / SUBPROGRAM AND MAIN / CALLING PROGRAM.
SAMPLE PROGRAM -2
This is a demo program to learn and understand the concept of sequential file handling and how to read data from input files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
IDENTIFICATION DIVISION.
PROGRAM–ID. SEQFILE.
ENVIRONMENT DIVISION.
INPUT–OUTPUT SECTION.
FILE–CONTROL.
SELECT INFL ASSIGN TO INFLDD
ORGANIZATION IS SEQUENTIAL
FILE STATUS FS–INFL.
SELECT OUTFL ASSIGN TO OUTFLDD
ORGANIZATION IS SEQUENTIAL
FILE STATUS FS–OUTFL.
DATA DIVISION.
FILE SECTION.
FD INFL.
01 INFL–REC.
05 STORE–ID PIC 9(05).
05 FILLER PIC X(01).
05 ITEM–ID PIC X(10).
05 FILLER PIC X(64).
FD OUTFL.
01 OUTFL–REC.
05 O–STORE–ID PIC 9(05).
05 DELIMIT PIC X(01).
05 O–ITEM–ID PIC X(10).
05 FILLER PIC X(64).
WORKING–STORAGE SECTION.
01 FS–INFL PIC X(02) VALUE SPACES.
88 FS–INFL–OK VALUE ’00’.
88 FS–INFL–EOF VALUE ’10’.
01 FS–OUTFL PIC X(02) VALUE SPACES.
88 FS–OUTFL–OK VALUE ’00’.
88 FS–OUTFL–EOF VALUE ’10’.
01 COUNTERS.
05 READ–COUNT PIC 9(2).
05 WRITE–COUNT PIC 9(2).
PROCEDURE DIVISION.
MAIN–PARA.
PERFORM OPEN–PARA THRU OPEN–EXIT–PARA.
PERFORM PROCESS–PARA THRU PROCESS–EXIT–PARA.
PERFORM CLOSE–PARA THRU CLOSE–EXIT–PARA.
STOP RUN.
OPEN–PARA.
INITIALIZE FS–INFL FS–OUTFL READ–COUNT WRITE–COUNT.
OPEN INPUT INFL
IF FS–INFL–OK
CONTINUE
ELSE
DISPLAY “FILE OPEN FAILED: “ FS–INFL
GO TO EXIT–PARA
END–IF.
OPEN OUTPUT OUTFL
IF FS–OUTFL–OK
CONTINUE
ELSE
DISPLAY “OUTPUT FILE OPEN FAILED: “ FS–OUTFL
GO TO EXIT–PARA
END–IF.
OPEN–EXIT–PARA.
EXIT.
PROCESS–PARA.
PERFORM UNTIL FS–INFL–EOF.
READ INFL
AT END
IF READ–COUNT < 1
DISPLAY ‘NO RECORDS PRESENT’
GO TO EXIT–PARA
END–IF
NOT AT END
PERFORM WRITE–PARA THRU WRITE–EXIT–PARA
END–READ
END–PERFORM.
PROCESS–EXIT–PARA.
EXIT.
WRITE–PARA.
ADD 1 TO READ–COUNT.
IF STORE–ID > 12346
MOVE “|” TO DELIMIT
MOVE STORE–ID TO O–STORE–ID
MOVE ITEM–ID TO O–ITEM–ID
WRITE OUTFL–REC
DISPLAY ‘HI’
END–IF.
WRITE–EXIT–PARA.
EXIT.
CLOSE–PARA.
CLOSE INFL.
CLOSE–EXIT–PARA.
EXIT.
EXIT–PARA.
EXIT PROGRAM.
|
THE OUTPUT WILL BE
1
2
3
4
5
|
000001 12347 | DETR12347
000002 12348 | JKLH67545
000003 12350 | KLJ443232
|
Relative File Handling in COBOL
- The relative file is also known as relative record data set or RRDS-file.
- The record in these files can be accessed by – RRN (Relative Record Number)
- It contains both random and sequential access.
- The record can be accessed in any order by declaring a ‘RECORD KEY‘. The key should be a numeric constant.
SYNTAX =>
1
2
3
4
5
6
7
8
9
10
11
12
13
|
FORMAT 1)
SELECT Logical – fl ASSIGNED TO physical – fl
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS R – KEY
FORMAT 2)
SELECT Logical – fl ASSIGNED TO physical – fl
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
RECORD KEY IS R – KEY
R – KEY must be defined in FD clause in the FILE SECTION.
|
Example:
Suppose we have a data of 9000 file record
1
2
3
4
5
6
7
8
9
10
|
SELECT Logical – fl ASSIGNED TO physical – fl
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL
RECORD KEY IS R – KEY
FD LOGICAL – FL.
01 LOGICAL – FL – REC
05 R – KEY PIC 9(4)
05 R – NAME PIC X(20)
|
In order to hold 1000 records, we must define the key with PIC clause 9 (4) that can hold a value from 0001 to 9999.