Key Takeaways — Chapter 11: Sequential File Processing
File Definition
-
SELECT...ASSIGN connects logical to physical. On z/OS, ASSIGN TO specifies a DD name; in GnuCOBOL, it specifies a file path or data name.
-
Always include FILE STATUS. Every SELECT must have a FILE STATUS clause. This is non-negotiable for production code.
-
Use BLOCK CONTAINS 0 RECORDS. Let the system choose optimal blocking. Never hard-code block sizes in new programs.
-
RECORDING MODE specifies the record format. F = fixed, V = variable, U = undefined. Most business files are F or V.
-
Use OPTIONAL for files that may not exist. Especially useful for configuration or override files.
I/O Lifecycle
-
The lifecycle is OPEN, READ/WRITE, CLOSE. Violating the order produces FILE STATUS codes in the 4x range (logic errors).
-
Check FILE STATUS after every operation. OPEN, READ, WRITE, REWRITE, CLOSE — all of them.
-
Use the priming read pattern. Read the first record before the loop. Read the next at the end of the loop body.
-
READ INTO copies to working storage. Preferred over plain READ because working-storage fields are stable between I/O operations.
-
WRITE FROM builds output in working storage. Cleaner than MOVE followed by WRITE.
-
WRITE uses the record name, not the file name. A common source of compilation errors for beginners.
Variable-Length Records
-
RECORD CONTAINS min TO max for variable-length. Combined with DEPENDING ON to track actual record length.
-
The DEPENDING ON variable is set by READ and used by WRITE. Always set it correctly before WRITE to avoid truncation or garbage bytes.
Multiple Record Types
-
Multiple 01-levels under one FD share the same buffer. They act as implicit REDEFINES of each other.
-
Use a record type indicator field. Typically the first byte. Route processing with EVALUATE TRUE on 88-level conditions.
-
Validate file structure. Check for: header before details, trailer present, no duplicate headers, control totals match.
Common Patterns
-
Copy with filter — Read, test a condition, write matching records.
-
Split — Read one file, write to multiple files based on content.
-
Merge — Read two sorted files, produce one sorted output.
-
Match-update — Read sorted transactions and sorted master, produce updated master.
-
Control break — Process sorted records, produce subtotals at group boundaries.
Performance
-
Blocking is the biggest performance lever. 18x improvement from optimal blocking is common.
-
Sequential access is faster than random access when you need most of the file. Sort first, then process sequentially.
-
Minimize file opens. Process everything you need in a single pass when possible.