In RPGLE (RPG IV), RECNO
is a file specification keyword used in the F-spec (File Specification) when you want to access a file by Relative Record Number (RRN). It allows you to read or write a record directly by its relative number, which is especially useful for physical files organized with RRN access.
✅ RECNO
Keyword in F-Spec
FMyFile IF E Disk RECNO(rrnVar)
Breakdown:
-
MyFile
: File name. -
I
: Input-capable file. -
F
: Full procedural file. -
E
: External file description. -
Disk
: Disk file. -
RECNO(rrnVar)
: Specifies the variable (rrnVar
) that holds the Relative Record Number to be used for direct positioning.
💡 Example
Dcl-f MyFile IF E Disk RECNO(rrn);
Dcl-s rrn Packed(10:0);
Dcl-s someField Char(10);
rrn = 5; // Position to 5th record
Read MyFile;
If not %eof(MyFile);
someField = MyField; // process the record
Endif;
-
The
RECNO(rrn)
positions the file pointer to RRN 5 before theREAD
. -
You can also use
CHAIN rrn MyFile;
to directly retrieve a record by RRN (if supported).
🔁 Updating a Specific RRN
rrn = 8;
Chain rrn MyFile;
If %found(MyFile);
MyField = 'NewValue';
Update MyFile;
Endif;
This allows you to read/update a specific record by its position in the file.
🧠 Important Notes
-
The file must be a physical file that supports RRN access.
-
Logical files or keyed files won't work directly with
RECNO
.