File I/O in ILE RPG: Five Operations, One Record Format
Declare a database file with DCL-F and its record format's fields appear in the program, much like a data structure. Input operations fill those fields from the file; output operations send their values to it. Every record-level file operation is a variation on that one idea.
Declare intent with USAGE
USAGE(*INPUT) reads, *OUTPUT adds, *UPDATE changes (and implies input), and *DELETE removes. A program that writes, updates, and deletes declares all three. The compiler and the system both enforce what the declaration allows.
READ and the primed loop
READ moves forward one record. At end of file it does not fail; it sets the condition that %EOF reports. The reliable loop reads once before the DOW and again at the bottom of the body. Omit either read and you skip the first record or never leave the loop.
read custmast;
dow not %eof(custmast);
dsply custName;
read custmast;
enddo;CHAIN and %FOUND
CHAIN retrieves a record by key. When no record matches, the fields are left untouched, so a program that skips the %FOUND test is working with whatever an earlier operation left behind.
WRITE, UPDATE, DELETE
WRITE adds a record from the current field values, so every field deserves a deliberate value first. UPDATE rewrites a record that was just retrieved. DELETE removes either the retrieved record or, in its keyed form, a record identified by key β checked afterward with %FOUND and, with the (E) extender, %ERROR.
Lesson 22 combines all five operations in a small customer-maintenance program and sets up Lesson 23's question: where does the compiler's knowledge of those fields come from?
Get the lesson
Lesson 22, βFile I/O: READ, WRITE, UPDATE, DELETE, and CHAIN,β is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series β all 18 lessons β is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.