Linux Notes: indexed-file-demo2.cob
- The information presented here is intended for educational use by qualified computer technologists.
- The information presented here is provided free of charge, as-is, with no warranty of any kind.
Edit: 2026-09-23
Back to:
Linux Notes: GnuCOBOL
indexed-file-demo2.cob
- The code:
* ===============================================================
* FILE : indexed-file-demo2.cob
* NOTES:
* 1) this program is a modified version of the demo cobol program
* sent by Roland.
*
* 2) It contains a few hacks (starting at line 100) so that it
* will compile as-is when using cobc (GnuCobol 3.2.0). The
* orginal failure is due to the fact that the original cobol
* spec has a default right limit of 72 (this comes from the
* days of punched cards). Compile this program like so:
*
* cobc -x indexed-file-demo2.cob
*
* Neil Rieck (2026-09-22)
* ===============================================================
IDENTIFICATION DIVISION.
PROGRAM-ID. INDEXED-DEMO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OPTIONAL EMP-FILE
ASSIGN TO "EMPLOYEE.DAT"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS EMP-ID
FILE STATUS IS WS-FS.
DATA DIVISION.
FILE SECTION.
FD EMP-FILE.
01 EMP-RECORD.
05 EMP-ID PIC 9(4).
05 EMP-NAME PIC X(20).
05 EMP-DEPT PIC X(10).
WORKING-STORAGE SECTION.
01 BUFFER132 PIC X(132).
01 WS-FS PIC XX.
01 WS-EOF-SWITCH PIC X VALUE "N".
88 END-OF-FILE VALUE "Y".
PROCEDURE DIVISION.
MAIN-LOGIC.
* 1. Open the file in I-O mode (reads, writes, and updates allowed)
OPEN I-O EMP-FILE
IF WS-FS = "35"
* File didn't exist; Open as OUTPUT to create it initially
OPEN OUTPUT EMP-FILE
CLOSE EMP-FILE
OPEN I-O EMP-FILE
END-IF
* 2. Write records to the indexed file
DISPLAY "--- WRITING RECORDS ---"
END-DISPLAY
PERFORM WRITE-SAMPLE-DATA
* 3. Random access lookup (Direct retrieval using key)
DISPLAY " "
DISPLAY "--- RANDOM ACCESS LOOKUP ---"
END-DISPLAY
PERFORM READ-RANDOM-RECORD
* 4. Sequential readout (Indexed files automatically sort by key)
DISPLAY " "
DISPLAY "--- SEQUENTIAL READOUT (SORTED BY KEY) ---"
PERFORM READ-ALL-SEQUENTIAL
CLOSE EMP-FILE
STOP RUN.
WRITE-SAMPLE-DATA.
MOVE 1003 TO EMP-ID
MOVE "Alice Smith" TO EMP-NAME
MOVE "Engineering" TO EMP-DEPT
PERFORM WRITE-RECORD
MOVE 1001 TO EMP-ID
MOVE "Bob Jones" TO EMP-NAME
MOVE "Marketing" TO EMP-DEPT
PERFORM WRITE-RECORD
MOVE 1002 TO EMP-ID
MOVE "Charlie Brown" TO EMP-NAME
MOVE "Finance" TO EMP-DEPT
PERFORM WRITE-RECORD.
WRITE-RECORD.
WRITE EMP-RECORD
INVALID KEY
DISPLAY "ERROR: Duplicate Key " EMP-ID "!"
NOT INVALID KEY
DISPLAY "Successfully wrote: " EMP-ID " - " EMP-NAME
END-WRITE.
READ-RANDOM-RECORD.
* Set the primary key to find exactly what we want
MOVE 1002 TO EMP-ID
READ EMP-FILE
INVALID KEY
DISPLAY "Record " EMP-ID " not found."
NOT INVALID KEY
*
* Caveat: cobc (guncobol) has a default right limit 72 (this goes
* back to the punched card days)
*
* 1) This original source line from another program fails (
* unless you compile with switch: -ftext-column=132
* DISPLAY "Found Key 1002 -> " EMP-NAME " (" EMP-DEPT ")"
* ----------------------------
* 2) This alternate syntax works
DISPLAY "Found Key 1002 -> " EMP-NAME
" (" EMP-DEPT ")"
END-DISPLAY
* ----------------------------
* 3) Copying to a temporary string also works
* STRING "Found Key 1002 -> " EMP-NAME
* " (" EMP-DEPT ")"
* INTO BUFFER132
* END-STRING
* DISPLAY BUFFER132
* ----------------------------
END-READ.
READ-ALL-SEQUENTIAL.
* Point the index back to the beginning of the file
MOVE 0000 TO EMP-ID
START EMP-FILE KEY IS GREATER THAN EMP-ID
INVALID KEY
DISPLAY "No records to read."
END-START
* Loop through the file sequentially
PERFORM UNTIL END-OF-FILE
READ EMP-FILE NEXT RECORD
AT END
SET END-OF-FILE TO TRUE
NOT AT END
DISPLAY "Seq Record: ID=" EMP-ID
" Name=" EMP-NAME
" Dept=" EMP-DEPT
END-READ
END-PERFORM.

Back to
Home
Neil Rieck
Waterloo, Ontario, Canada.