---
title: "COBOL Indexed Files Useful Sample Logic"
description: "The best way you can use Indexed file Read syntax in your project with less efforts. This is a way of writing good syntax in COBOL."
url: https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/
date: 2017-03-21
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2017/03/cobol-examples.jpg?fit=1200%2C700&ssl=1
categories: ["COBOL"]
tags: ["COBOL Indexed File", "COBOL Tips and Tricks"]
type: post
lang: en
---

# COBOL Indexed Files Useful Sample Logic

Here is sample logic on [how to](https://srinimf.com/2022/04/24/vsam-program-how-to-write-correctly/) define, read, and write [indexed files](https://srinimf.com/2015/10/24/complete-information-on-cobol-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](https://srinimf.com/2015/02/07/vsam-file-reading-in-cobol-program/).

## Indexed files

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]
```

Related Posts

- [COBOL Sequential Vs. Indexed: These are Top Differences](https://srinimf.com/2022/02/05/how-to-write-a-cobol-program-using-sequential-and-indexed-files/)DateFebruary 5, 2022
- [COBOL: Here’s All About Relative – And Indexed Files](https://srinimf.com/2015/03/23/tough-cobol-interview-questions/)DateMarch 23, 2015
- [COBOL READ NEXT – How to Code Correctly Unlock now](https://srinimf.com/2016/03/17/indexed-files/)DateMarch 17, 2016

Get new content delivered directly to your inbox.

Type your email…

Sign up

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2017%2F03%2F21%2Fhow-to-read-indexed-file-syntax%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2017%2F03%2Fcobol-examples.jpg%3Ffit%3D1200%252C700%26ssl%3D1&description=COBOL%20Indexed%20Files%20Useful%20Sample%20Logic)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20COBOL%20Indexed%20Files%20Useful%20Sample%20Logic&body=https%3A%2F%2Fsrinimf.com%2F2017%2F03%2F21%2Fhow-to-read-indexed-file-syntax%2F&share=email)
-

### Like this:

Like Loading…
