Here is sample logic on how to define, read, and write indexed files in COBOL.
In COBOL organization is Indexed, you can say it a method to define these files. The access mode can be Sequential/Random/Dynamic.
In the below example the first step I have mentioned “Organization is Indexed”. The access mode can be Random or Dynamic.
Select filename-1 Assign to Imp-1 Organization is indexed Access Mode is Random Record Key is Part-1
***You can open indexed files with INPUT, OUTPUT, IO and EXTEND modes. If you want to read and write into the same file then you can use I/O mode. This is top interview question. These file modes also applicable to sequential files.
Points to Remember: Access mode is Random. That means you cannot access the record sequentially.
How to Read & Write Indexed File
In the files definition of SELECT, we mentioned ‘Random Access’. So, you need to give NEXT in the read statement. Precisely, for Dynamic and Random access modes you need ‘NEXT’. For Access mode is sequential the NEXT not required. Below is the way to read indexed file till end of the file.
READ file-name [NEXT] RECORD [INTO data-name] [AT END imperative-statement-1] [NOT AT END imperative-statement-2] [END-READ]
Write Indexed Files
WRITE record-name [FROM data-name] [INVALID KEY imperative-statement-1] [NOT INVALID KEY imperative-statement-2] [END-WRITE]
Get new content delivered directly to your inbox.