Back to: ILE RPG in Easy Bytes

Unit 8 introduces you to ILE RPG’s operations for reading, writing, and updating records. It also examines both sequential and random file access techniques. In addition, the chapter discusses file maintenance—adding, deleting, and changing records in a file—and record locking considerations in update procedures.
This course is a unit of the ILE RPG in Easy Bytes series, a complete introductory ILE RPG programming self-guided tutorial.
- Before starting this course you must complete the required prerequisite course: 7. Dates – ILE RPG in Easy Bytes
One-Time Payment
Members Only
Instead of paying for each unit individually, you may purchase a membership which will automatically enroll you at a discount in the entire “ILE RPG in Easy Bytes” series.
Reading Records
ILE RPG offers several alternative operations for accessing data from full procedural database files. Several of these operations are appropriate for sequential processing. You can use others for random access processing.
Often, however, you want to be able to read a specific record, determined by its key value, without having to read through the file sequentially to reach that record. This kind of access is called random access. Random access lets you reach into a file and extract just the record you want.
Creating and Modifying Records
In addition to reading records, an RPG program can deal with output (i.e., writing new records to database files).
File maintenance, or updating, involves adding or deleting records from database files, or changing the information in database records to keep the information current and correct. Records that do not exist cannot be changed or deleted. If a file has unique keys, you should not add a second record with the same key to the file.
Otherwise, it is possible that if two users access the same record for update, make changes in the record, and then rewrite it to the file, one of the user’s changes might get lost—a condition sometimes called phantom updates. Two approaches you can use to deal with this type of problem are file locking and record locking. The easiest kind of locking is to limit access to a file to one user at a time—a condition known as file locking.
Other File Topics
RPG automatically opens your files at the beginning of processing and then closes them all at the end of processing. If your program needs access to a file for only a portion of its total running time, you should take control of the file opening and closing rather than letting RPG manage those tasks for you. ILE RPG includes two operations to give you this capability:
Accordingly, read the scenarios that follow and study the program solutions to develop a sense of when to use the various I/O operations. Example 1 In the first scenario, a company has decided to give all its employees a 5 percent pay raise. Assume that an externally described master file of employees (Empmaster) exists, that the record format within the database file is Emprecord, and that the pay field is Pay.
The fixed-format File specification (F-spec) is comparable to the Dcl-f (Declare File) instruction. Let’s look at some of the columnar entries appropriate to this chapter. We’ll primarily examine the additional entries not previously covered. You have already used the Except (Calculation time output) operation to write to printer files, but you can also use Except to write new records to a database file. When used with fixed-format C-specs, the file read operations (Read, Reade, Readp, and Readpe) can set resulting indicators to flag an error or end-of-file condition.
- Before starting this course you must complete the required prerequisite course: 7. Dates – ILE RPG in Easy Bytes