Key Takeaways — Chapter 11: Sequential File Processing

File Definition

  1. 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.

  2. Always include FILE STATUS. Every SELECT must have a FILE STATUS clause. This is non-negotiable for production code.

  3. Use BLOCK CONTAINS 0 RECORDS. Let the system choose optimal blocking. Never hard-code block sizes in new programs.

  4. RECORDING MODE specifies the record format. F = fixed, V = variable, U = undefined. Most business files are F or V.

  5. Use OPTIONAL for files that may not exist. Especially useful for configuration or override files.

I/O Lifecycle

  1. The lifecycle is OPEN, READ/WRITE, CLOSE. Violating the order produces FILE STATUS codes in the 4x range (logic errors).

  2. Check FILE STATUS after every operation. OPEN, READ, WRITE, REWRITE, CLOSE — all of them.

  3. Use the priming read pattern. Read the first record before the loop. Read the next at the end of the loop body.

  4. READ INTO copies to working storage. Preferred over plain READ because working-storage fields are stable between I/O operations.

  5. WRITE FROM builds output in working storage. Cleaner than MOVE followed by WRITE.

  6. WRITE uses the record name, not the file name. A common source of compilation errors for beginners.

Variable-Length Records

  1. RECORD CONTAINS min TO max for variable-length. Combined with DEPENDING ON to track actual record length.

  2. 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

  1. Multiple 01-levels under one FD share the same buffer. They act as implicit REDEFINES of each other.

  2. Use a record type indicator field. Typically the first byte. Route processing with EVALUATE TRUE on 88-level conditions.

  3. Validate file structure. Check for: header before details, trailer present, no duplicate headers, control totals match.

Common Patterns

  1. Copy with filter — Read, test a condition, write matching records.

  2. Split — Read one file, write to multiple files based on content.

  3. Merge — Read two sorted files, produce one sorted output.

  4. Match-update — Read sorted transactions and sorted master, produce updated master.

  5. Control break — Process sorted records, produce subtotals at group boundaries.

Performance

  1. Blocking is the biggest performance lever. 18x improvement from optimal blocking is common.

  2. Sequential access is faster than random access when you need most of the file. Sort first, then process sequentially.

  3. Minimize file opens. Process everything you need in a single pass when possible.