Choosing the right file organization is one of the most important decisions in COBOL application design. The file organization you select determines how records are stored, how they can be accessed, and what operations are possible. This choice directly impacts application performance, storage requirements, and maintenance complexity.
This guide helps you understand when to use each file organization type, compares their characteristics, and provides practical decision-making criteria for selecting the best organization for your specific application needs.
COBOL supports three primary file organizations:
Each organization has distinct characteristics, advantages, and limitations. Understanding these differences is essential for making informed design decisions.
The following table compares the key characteristics of each file organization:
| Characteristic | Sequential | Indexed (KSDS) | Relative (RRDS) |
|---|---|---|---|
| Access Methods | Sequential only | Sequential, Random, Dynamic | Sequential, Random, Dynamic |
| Record Length | Fixed or Variable | Fixed or Variable | Fixed only |
| Key Requirement | None | Primary key required | Relative key (record number) |
| Random Access | Not supported | By key value | By record number |
| Update Support | Limited (must recreate file) | Full support (READ, REWRITE, DELETE) | Full support (READ, REWRITE, DELETE) |
| Storage Overhead | Minimal | 30-50% for indexes | Minimal (may waste empty slots) |
| Performance (Sequential) | Fastest | Good (with index overhead) | Good |
| Performance (Random) | Not applicable | Fast (via index) | Very fast (direct access) |
| Alternate Keys | Not supported | Supported | Not supported |
| Best For | Batch processing, logs, archives | Transaction processing, master files | Position-based access, fixed records |
Sequential files store records in the order they are written. Records must be accessed sequentially from the beginning of the file. This is the simplest file organization with minimal overhead.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546IDENTIFICATION DIVISION. PROGRAM-ID. SEQUENTIAL-PROCESSING. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO "TRANS.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE. 01 TRANSACTION-RECORD. 05 TRANS-DATE PIC 9(8). 05 TRANS-AMOUNT PIC S9(7)V99. 05 TRANS-DESCRIPTION PIC X(50). WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 END-OF-FILE VALUE "10". 01 TOTAL-AMOUNT PIC S9(9)V99 VALUE ZEROS. PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT TRANSACTION-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF PERFORM PROCESS-TRANSACTIONS UNTIL END-OF-FILE CLOSE TRANSACTION-FILE DISPLAY "Total amount: " TOTAL-AMOUNT STOP RUN. PROCESS-TRANSACTIONS. READ TRANSACTION-FILE AT END CONTINUE NOT AT END ADD TRANS-AMOUNT TO TOTAL-AMOUNT DISPLAY "Processed: " TRANS-DESCRIPTION END-READ.
This example demonstrates typical sequential file processing: opening the file, reading records one by one until end of file, processing each record, and closing the file. Sequential access is straightforward and efficient for this use case.
Indexed files use keys to organize records and maintain indexes that allow direct access to records by key value. They support both sequential and random access, making them versatile for many applications.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849IDENTIFICATION DIVISION. PROGRAM-ID. INDEXED-ACCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC S9(7)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 88 RECORD-FOUND VALUE "00". 88 RECORD-NOT-FOUND VALUE "23". 01 SEARCH-ID PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF DISPLAY "Enter customer ID to search: " ACCEPT SEARCH-ID MOVE SEARCH-ID TO CUSTOMER-ID READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer " SEARCH-ID " not found" NOT INVALID KEY DISPLAY "Customer found:" DISPLAY " Name: " CUSTOMER-NAME DISPLAY " Balance: " CUSTOMER-BALANCE END-READ CLOSE CUSTOMER-FILE STOP RUN.
This example shows random access to an indexed file. The program can directly access any customer record by setting the key (CUSTOMER-ID) and reading. The index allows the system to locate the record without reading through preceding records.
Relative files use relative record numbers to identify record positions. Records are stored in fixed-size slots, allowing direct access by record number. This organization is efficient for position-based access patterns.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647IDENTIFICATION DIVISION. PROGRAM-ID. RELATIVE-ACCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INVENTORY-FILE ASSIGN TO "INVENTORY.DAT" ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS RECORD-NUMBER FILE STATUS IS FILE-STATUS-CODE. DATA DIVISION. FILE SECTION. FD INVENTORY-FILE. 01 INVENTORY-RECORD. 05 ITEM-CODE PIC X(10). 05 ITEM-DESCRIPTION PIC X(40). 05 QUANTITY-ON-HAND PIC 9(5). 05 UNIT-PRICE PIC S9(5)V99. WORKING-STORAGE SECTION. 01 FILE-STATUS-CODE PIC XX. 01 RECORD-NUMBER PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O INVENTORY-FILE IF FILE-STATUS-CODE NOT = "00" DISPLAY "Error opening file: " FILE-STATUS-CODE STOP RUN END-IF *> Access record at position 100 MOVE 100 TO RECORD-NUMBER READ INVENTORY-FILE INVALID KEY DISPLAY "Record " RECORD-NUMBER " not found" NOT INVALID KEY DISPLAY "Item: " ITEM-CODE DISPLAY "Description: " ITEM-DESCRIPTION DISPLAY "Quantity: " QUANTITY-ON-HAND DISPLAY "Price: " UNIT-PRICE END-READ CLOSE INVENTORY-FILE STOP RUN.
This example demonstrates direct access to a relative file by record number. The relative key (RECORD-NUMBER) specifies which slot to access, allowing very fast direct access when you know the record position.
Use this decision tree to help choose the appropriate file organization:
Question: How will records be accessed?
Question: How frequently will records be updated?
Question: What are the record characteristics?
Question: What are the performance priorities?
Requirements: Look up customers by ID, update customer information, generate reports
Choice: Indexed File (VSAM KSDS)
Reasoning: Requires random access by customer ID, frequent updates, and both sequential (reports) and random (lookups) access patterns. Indexed files provide the necessary flexibility.
Requirements: Append transactions, read sequentially for audit reports, rarely updated
Choice: Sequential File
Reasoning: Only sequential access needed, records are appended (not inserted), no random access required. Sequential files are simplest and most efficient for this use case.
Requirements: Access items by bin location (position-based), fixed record size, frequent updates
Choice: Relative File (VSAM RRDS)
Reasoning: Access is position-based (bin number maps to record number), records are fixed-length, and relative files provide efficient direct access by position.
Sequential files provide the best performance for sequential processing:
Indexed files balance sequential and random access performance:
Relative files excel at direct position-based access:
Sometimes you need to change file organization. This requires creating a new file and copying data:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273IDENTIFICATION DIVISION. PROGRAM-ID. CONVERT-FILE-ORG. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. *> Source: Sequential file SELECT SOURCE-FILE ASSIGN TO "SOURCE.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS SOURCE-STATUS. *> Target: Indexed file SELECT TARGET-FILE ASSIGN TO "TARGET.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS SEQUENTIAL RECORD KEY IS RECORD-KEY FILE STATUS IS TARGET-STATUS. DATA DIVISION. FILE SECTION. FD SOURCE-FILE. 01 SOURCE-RECORD. 05 RECORD-KEY PIC 9(5). 05 RECORD-DATA PIC X(75). FD TARGET-FILE. 01 TARGET-RECORD. 05 RECORD-KEY PIC 9(5). 05 RECORD-DATA PIC X(75). WORKING-STORAGE SECTION. 01 SOURCE-STATUS PIC XX. 88 END-OF-SOURCE VALUE "10". 01 TARGET-STATUS PIC XX. 01 RECORD-COUNT PIC 9(6) VALUE ZEROS. PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT SOURCE-FILE IF SOURCE-STATUS NOT = "00" DISPLAY "Error opening source file: " SOURCE-STATUS STOP RUN END-IF OPEN OUTPUT TARGET-FILE IF TARGET-STATUS NOT = "00" DISPLAY "Error opening target file: " TARGET-STATUS CLOSE SOURCE-FILE STOP RUN END-IF PERFORM COPY-RECORDS UNTIL END-OF-SOURCE CLOSE SOURCE-FILE CLOSE TARGET-FILE DISPLAY "Conversion complete. Records copied: " RECORD-COUNT STOP RUN. COPY-RECORDS. READ SOURCE-FILE AT END CONTINUE NOT AT END MOVE SOURCE-RECORD TO TARGET-RECORD WRITE TARGET-RECORD IF TARGET-STATUS NOT = "00" DISPLAY "Error writing record: " TARGET-STATUS ELSE ADD 1 TO RECORD-COUNT END-IF END-READ.
This example shows how to convert a sequential file to an indexed file. The program reads from the sequential source file and writes to the indexed target file. Similar patterns can be used for other conversions.
Choosing the right file organization is critical for COBOL application success. Sequential files excel at batch processing, indexed files provide flexibility for transaction processing, and relative files offer efficient position-based access. Consider your access patterns, update requirements, record characteristics, and performance needs when making your choice.
Remember:
There's no one-size-fits-all solution. The best file organization depends on your specific application requirements. Take time to analyze your access patterns, test with realistic data, and choose the organization that best fits your needs.
1. Which file organization is best for a transaction processing system that needs to look up customer records by customer ID?
2. What is the primary advantage of sequential file organization?
3. Which file organization requires fixed-length records?
4. What additional storage overhead do indexed files require?
5. Which file organization supports both sequential and random access?
6. For a log file that only appends new records and is read sequentially from beginning to end, which file organization is most appropriate?
7. What is a key consideration when choosing between indexed and relative file organizations?
8. Which file organization would be best for a file that is frequently updated with records being added, modified, and deleted?
Learn detailed technical specifications and syntax for each file organization type including sequential, indexed, and relative files.
Understand sequential, random, and dynamic access modes and how they work with different file organizations.
Learn performance optimization techniques for different file organizations including buffering, blocking, and tuning strategies.
Master READ, WRITE, REWRITE, and DELETE operations and how they work with different file organizations.