Exercises — Chapter 11: Sequential File Processing

Exercise 11.1: Basic Sequential Read (Beginner)

Write a program that reads a sequential file of product records (product ID, name, category, price, quantity) and displays each record. Include: - FILE STATUS checking after OPEN and every READ - A priming read before the processing loop - Record count displayed at termination - Appropriate handling of an empty file

Deliverables: PROD-READ.cbl, sample data file

Exercise 11.2: Sequential Write with Error Checking (Beginner)

Write a program that creates a sequential output file containing employee records. Hard-code at least 10 employee records in WORKING-STORAGE and write each to the output file. Include: - FILE STATUS checking after OPEN, every WRITE, and CLOSE - A record counter - Error handling if the WRITE fails (status '34' — file full)

Deliverables: EMP-WRITE.cbl

Exercise 11.3: Copy with Filter (Intermediate)

Write a program that reads a transaction file and copies only transactions above a threshold amount to an output file. The threshold should be defined in WORKING-STORAGE. Include: - Full defensive FILE STATUS checking - Counts for records read, records written, and records skipped - A summary display at termination - Return code: 0 if all OK, 4 if any records were skipped

Deliverables: TXN-FILT.cbl, sample data file

Exercise 11.4: File Split (Intermediate)

Read a single input file containing customer records with a REGION field ('N' = North, 'S' = South, 'E' = East, 'W' = West). Split the records into four output files, one per region. Records with an invalid region code go to a reject file. Include: - Five output files (four regions plus rejects) - FILE STATUS checking on all six files - Per-file record counts - Summary statistics at termination

Deliverables: CUST-SPLIT.cbl, sample data file

Exercise 11.5: Header-Detail-Trailer Processing (Intermediate)

Create and process a file with header, detail, and trailer records: 1. Write a "generator" program that creates a header-detail-trailer file with at least 50 detail records 2. Write a "processor" program that: - Validates the header (must be the first record) - Processes detail records (validates each one) - Validates the trailer (record count and amount total) - Detects structural errors (detail before header, missing trailer, duplicate header)

Deliverables: HDT-GEN.cbl, HDT-PROC.cbl

Exercise 11.6: Variable-Length Records (Intermediate)

Create a program that works with variable-length records: 1. Define a student record where the fixed portion is 50 bytes (ID, name, major) and the variable portion contains 0-8 course entries (15 bytes each: course code + grade) 2. Write a program that creates the variable-length file with at least 20 students having different numbers of courses 3. Write a reader program that displays each student and their course count, and computes the average number of courses per student

Deliverables: VLEN-GEN.cbl, VLEN-READ.cbl

Exercise 11.7: Report Generation (Intermediate)

Write a program that reads a transaction file and produces a formatted report with: - Page headers (report title, date, page number) - Column headers - Detail lines with formatted amounts (edited PIC with commas, decimal point, sign) - Page breaks at 55 lines per page - Summary totals at the end (total amount, record count, average) - Use WRITE AFTER ADVANCING for line spacing and page breaks

Deliverables: TXN-RPT.cbl, sample data file

Exercise 11.8: Sequential Match-Update (Advanced)

Implement the match-update pattern: 1. Create a sorted master file (customer records, sorted by customer ID) 2. Create a sorted transaction file (updates, sorted by customer ID) 3. Write a program that produces a new master file by applying transactions to matching master records 4. Handle: matched records (update), unmatched transactions (new customers), unmatched masters (unchanged) 5. Produce an audit report showing all actions taken

Use HIGH-VALUES sentinel technique for end-of-file handling on both files.

Deliverables: MATCH-UPD.cbl, master data file, transaction data file

Exercise 11.9: Multi-File Merge (Advanced)

Write a program that merges three sorted input files into one sorted output file. All files are sorted by the same key. Handle: - End-of-file on each file independently - Duplicate keys across files (keep all records) - Empty files (one or more input files may be empty) - Full FILE STATUS checking on all four files

Deliverables: MERGE-3.cbl, three sorted input files

Exercise 11.10: Complete Batch Processing System (Capstone)

Build a complete batch processing system for a small retail order system: 1. ORD-INTAKE — Reads a header-detail-trailer order file, validates each order, writes valid orders to an output file and rejects to a reject file 2. ORD-REPORT — Reads the valid order file and produces a formatted report with page breaks, subtotals by department, and grand totals 3. ORD-SUMMARY — Reads the valid order file and produces a summary file with one record per department containing the order count and total amount

All three programs should use shared copybooks for the order record layout and common working-storage fields.

Deliverables: Three .cbl programs, all required copybooks, sample data files, JCL (or shell script) to run the three programs in sequence

Reflection Questions

  1. Why is the priming read pattern preferred over reading inside the condition of the PERFORM UNTIL? What subtle bugs can occur without a priming read?

  2. Compare READ INTO with a plain READ followed by MOVE. Are they truly equivalent in all cases? What edge cases might differ?

  3. When processing a header-detail-trailer file, what should your program do if it reaches end-of-file without encountering a trailer record?

  4. You need to process a 50-million-record sequential file. What performance considerations should guide your program design?