Here is correct syntax to use sequential and indexed files in COBOL Explained the differences between these two files.
Sequential and Indexed Files
Below is the syntax to write the SELECT statement in the COBOL program.
Sequntial files
---------------
SELECT file-name ASSIGN TO system-name
[ORGANIZATION IS SEQUENTIAL]
[ACCESS MODE IS SEQUENTIAL]
[FILE STATUS IS data-name]
Indexed file with Reacord Key
---------------------------
SELECT INVMAST ASSIGN TO INVMAST
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS IM-ITEM-NO.
SELECT INVMAST ASSIGN TO INVMAST
ORGANIZATION IS INDEXED
ACCESS IS RANDOM
RECORD KEY IS IM-ITEM-NO.
1. For indexed files, you need to specify RECORD KEY in SELECT
2. For indexed files, when the access is ‘sequential, you can use READ to read a file
3. For indexed files, when the access is ‘random’, you need to use START to read a file.
Syntax to write file description. It’s the same for both sequential and indexed files.
FD INVMAST
RECORD CONTAINS 70 CHARACTERS.
Syntax to Open a file. It’s the same for both sequential and indexed files.
OPEN INPUT file1
OPEN OUTPUT file2
OPEN I-O file3
OPEN EXTEND file4
Syntax to read a file.
/* Sequential files */
READ file-name [NEXT] RECORD [INTO data-name]
[AT END imperative-statement-1]
[NOT AT END imperative-statement-2]
[END-READ]
/* indexed files */
READ file-name RECORD [INTO data-name]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-READ]
Syntax to write a record.
/*Sequential files */
WRITE record-name [FROM data-name]
[END-WRITE]
REWRITE record-name [FROM data-name]
[END-REWRITE]
/* Indexed files */
WRITE record-name [FROM data-name]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-WRITE]
REWRITE record-name [FROM data-name]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-REWRITE]
Syntax to Close a file. It’s the same for both sequential and indexed files.
CLOSE file-name-1 ...
Related posts