Basic File Handling: Sequential Files
COBOL Fundamentals
Chapter 8 · Basic File Handling: Sequential Files
Every real record processed so far has lived only in WORKING-STORAGE, gone the moment the program ends. Real COBOL programs exist to read and write actual files — and a sequential file is COBOL's own most basic, most common shape: records genuinely contiguous, read one after another from the front, "similarly to a linked list."
Three Places a File Touches a Program
A real COBOL file spans three separate divisions, each doing a genuinely different job:
| Division | What It Declares |
|---|---|
| ENVIRONMENT DIVISION | Which real external file this program talks to (SELECT/ASSIGN) |
| DATA DIVISION | What one real record inside that file looks like (the FD and its record layout) |
| PROCEDURE DIVISION | The real verbs that actually move data in and out (OPEN, READ, WRITE, CLOSE) |
SELECT & FD: Declaring the File
This chapter's real CUSTOMER-RECORD deliberately mirrors Chapter 3's own WS-CUSTOMER-RECORD — a real record layout doesn't change shape just because it's now coming from a file instead of being built by hand in WORKING-STORAGE.
OPEN: Real Access Modes
| Mode | What It Genuinely Allows |
|---|---|
| INPUT | Reading only, from a file that already exists |
| OUTPUT | Writing only; a real, existing file's contents are replaced entirely |
| I-O | Both reading and writing on the same open file (not valid for a real line-sequential file) |
| EXTEND | Writing new records appended after whatever a real file already contains |
READ ... AT END & the Real Priming-Read Pattern
A real READ pulls the next record in, and its AT END phrase fires the moment a read genuinely goes past the last record in the file:
READ is a real statement, not an expression a loop condition can call directly. Chapter 5's own PERFORM UNTIL can't say "loop until the next READ hits end-of-file" in one step — it needs a genuine value to test. The standard real fix is the priming read: read once before the loop starts, then read again as the very last thing the loop body does, so WS-EOF-FLAG always reflects the most recent real read by the time the loop condition is checked again.
WRITE: Real Records, Not Files
WRITE always names the real 01-level record — CUSTOMER-RECORD — never the file itself. Writing WRITE CUSTOMER-FILE is a genuine compile error: COBOL writes whatever record is currently sitting in that record's own storage, and the file name never appears as the object being written.
CLOSE
A real, plain CLOSE releases the file — every file genuinely opened in a program needs a matching close before the program ends.
FILE STATUS: Real Result Codes
I/O status codes were a real COBOL-85 addition — the same standard that added Chapter 5's EVALUATE, Chapter 7's reference modification, and inline PERFORM. A real two-character FILE STATUS field, declared in WORKING-STORAGE and named in the file's own SELECT clause, is set after every single file operation:
| Code | Real Meaning |
|---|---|
| 00 | Operation genuinely successful |
| 10 | End of file reached — the real code behind every AT END trigger |
| 23 | Record not found (indexed/relative files) |
| 35 | OPEN INPUT/I-O attempted against a file that genuinely doesn't exist |
Hands-On Exercises
Using this chapter's own real material, explain in your own words why COBOL needs a priming read before a PERFORM UNTIL loop rather than simply writing PERFORM UNTIL the next READ reaches end-of-file directly in the loop's own condition.
📄 View solutionUsing this chapter's own real material, explain in your own words what would genuinely happen if a program opened CUSTOMER-FILE with OPEN OUTPUT when the goal was actually to read the file's existing records.
📄 View solutionUsing this chapter's own real material, explain in your own words why WS-FILE-STATUS being "00" after a READ statement genuinely can't be relied on forever within a real priming-read loop, and what real status value eventually replaces it.
📄 View solutionChapter 8 Quick Reference
- SELECT ... ASSIGN TO (ENVIRONMENT DIVISION) — names the real external file; FD (DATA DIVISION) — describes one real record's layout
- OPEN INPUT/OUTPUT/I-O/EXTEND — real access modes; OUTPUT genuinely replaces existing content
- READ ... AT END — fires once a read genuinely passes the last record; needs a real priming read before a PERFORM UNTIL loop
- WRITE — always names the real 01-level record, never the file itself
- CLOSE — every genuinely opened file needs a matching close
- FILE STATUS (COBOL-85) — real 2-character result code after every file operation; 00 success, 10 end of file, 35 file not found on OPEN