Read Prior Record (READP)
The Read Prior Record (READP) command reads the previous record from where a file is currently pointing.
This command never runs in the traditional sense and will return an error if an attempt is made to run the command. The CLF precompilers, when they encounter this command in a CL source program, replace the command and generate CL instructions directly into the compiled CL program.
Restrictions:
- This command can only be used in a CL program that is created using a CLF precompiler command. The CLF precompiler commands are CRTBNDCLF, CRTCLFMOD, and CRTCLFPGM.
| Keyword | Description | Choices | Notes |
| FILEID |
File identifier |
Simple name |
Optional, Positional 1 |
| RCDFMT |
Record format |
Character value, *ONLY |
Optional |
| EOF |
End of file |
Logical value |
Optional |
| ERR |
Error found |
Logical value |
Optional |
| RCDLCK |
Record lock |
*OPNF, *NO |
Optional |
| OPNPGM |
Program opening file |
Single values: *CURPGM Other values: Qualified object name |
Optional |
| Qualifier 1: Program opening file |
Name |
| Qualifier 2: Library |
Name, *CURLIB |
File identifier (FILEID)
Specifies the file identifier that was used on a previous Open File for Processing (OPEN) or Open File using CLF (OPNFCLF) command in the application.
This parameter is used by CLF commands to identify the file to be processed by the command.
If the file is declared using the DCLFCLF command this parameter is optional if the RCDFMT parameter specifies a record format name that is associated with one and only one file ID.
- simple-name
- Specify a name that matches the FILEID parameter value of a previous OPEN or OPNFCLF command.
Record format (RCDFMT)
Specifies the name of the record format to be used.
- *ONLY
- There is only one record format defined for the file. This record format will be used by the command.
- character-value
- Specify the name of the record format to be used.
End of file (EOF)
Specifies a logical CL variable to receive the end-of-file status. If the variable value is equal to '1' (true) then end-of-file was reached when performing the read command. If the variable value is equal to '0' (false) then end-of-file was not reached.
This CL variable is typically declared by either the Declare Indicators using CLF (DCLINDCLF) command or the Generate Indicators using CLF (GENINDCLF) command. These commands would have specified CLFIND(*YES). When using these commands the name of the declared CL variable is &EOF.
If no CL variable is specified and an end of file condition is encountered the application program will be sent escape message VC2501C.
- logical-value
- Specify the name of a CL variable declared as TYPE(*LGL).
Error found (ERR)
Specifies a CL logical variable to receive general error status. If the variable value is equal to '1' (true) then an error was found when performing the command. If the variable value is equal to '0' (false) then no error was found when performing the command.
This CL variable can be declared by the Declare Indicators using CLF (DCLINDCLF) command, the Generate Indicators using CLF (GENINDCLF) command, or by you using the Declare CL Variable (DCL) command. The two CLF commands would have specified CLFIND(*YES). When using the CLF commands the name of the CL variable is &ERR.
If no CL variable is specified and an error condition is encountered the application program will be sent an escape message.
- logical-value
- Specify the name of a CL variable declared as TYPE(*LGL).
Record lock (RCDLCK)
Specifies if the record being read should be locked.
- *OPNF
- The record being read will be locked based on the value specifed for the USAGE parameter when the file was opened. If the file is open with USAGE(*BOTH) then the record will be locked. Otherwise the record will not be locked.
- *NO
- The record will not be locked. If the job currently holds a lock on a record within the file the pre-existing lock will continue to be held. To release a pre-existing lock you can use either the Release a Record (UNLOCK) command or the Release Record using CLF (RLSRCDCLF) command.
Program opening file (OPNPGM)
The same FILEID value can be in use across multiple programs within a job. Each instance of a file open is uniquely identifed by the OPNPGM parameter. This parameter specifes the application program that initially opened the file using the Declare File using CLF (OPEN) or the Open File using CLF (OPNFCLF) command.
All CLF commands that reference the same FILEID and OPNPGM combination share the same view of the file. If one program causes the position within the file to change all programs see that change. If one program closes the file then the file is closed to all programs.
The combination of FILEID and OPNPGM must be unique within the activation group the application program is running in.
If the program name is *CURPGM then the library qualifier for this parameter is ignored. The library of the current program will be used.
Single values: Program opening file
- *CURPGM
- The current program opened the file associated with FILEID. It is this file open instance that is to be used.
Qualifier 1: Program opening file
- name
- Specify the name of the program that initially opened the file identified by FILEID. It is this file open instance that is to be used.
Qualifier 2: Library
- *CURLIB
- The current library for the thread is used. If there is no current library for the thread then library QGPL is used.
- name
- Specify the name of the library where the program that initially opened the file is located.
Examples for READP
Example 1: Reading a Previous Record
ReadP FileID(VC2DPT) EOF(&EOF)
The READP command will read the previous record from file VC2DPT. The CL logical variable &EOF will be set to true when attempting to read a record prior to the first record in the file.
Example 2: Reading Previous Records
Pgm
/******************************************************/
/* This program displays employee records in reverse */
/* order by employee number. The program positions */
/* to the end of the VC2EMP file and reads all */
/* records, in key sequence, using the READP command. */
/******************************************************/
/******************************************************/
/* Declare the Employee master file VC2EMP and */
/* indicators for CLF I/O related commands. */
/******************************************************/
File FileID(VC2EMP)
Inds CLFInd(*Yes)
/******************************************************/
/* Declare character version of employee number */
/******************************************************/
Dcl Var(&EmpNo) Type(*Char) Len(5)
/******************************************************/
/* Open VC2EMP with keyed access */
/******************************************************/
Open VC2EMP AccMth(*Key)
/******************************************************/
/* Position to end of file */
/******************************************************/
SetGT *Hival VC2EMP
/******************************************************/
/* Read all records until end of file found. Display */
/* employee number and first name. */
/******************************************************/
ReadP VC2EMP EOF(&EOF)
DoWhile Cond(*Not &EOF)
ChgVar Var(&EmpNo) Value(&EmpNbr)
SndPgmMsg Msg(&EmpNo *BCat &EmpFName)
ReadP VC2EMP EOF(&EOF)
EndDo
/******************************************************/
/* When done close the file and return */
/******************************************************/
Close VC2EMP
EndPgm
The source for this example can be found in member RPG_READP of source file VC2CLSRC in library VC2CLF. The source for file VC2EMP can be found in member VC2EMP of source file QDDSSRC in library VC2CLF.
The VC2EMP database file should currently exist in library VC2CLF. To create DEV_READP into QTEMP you can use the commands
AddLibLE Lib(VC2CLF)
CrtBndCLF Pgm(QTEMP/RPG_READP) SrcFile(VC2CLSRC)
Error messages for READP
*ESCAPE Messages
- CPF0001
- Error found on &1 command.
- VC2501A
- File &1 is not open for access by relative record number.
- VC2501B
- Record not found in file &1.
- VC2501C
- End of file for file &1.
- VC25021
- The &4 command was not successful when accessing File ID &2.
- VC25025
- Target datatype of &1 not recognized.
- VC2502E
- File ID &1 is not open.
- VC25065
- RCDBUF parameter is missing on &2 command.
- VC29002
- Function did not complete. See previously listed messages related to possible user errors.
- VC29003
- Function did not complete. See previously listed messages for possible cause.
- VC29004
- Function did not complete. See previously listed messages for possible cause.
- VC29005
- Function did not complete. Contact your service provider.
- VC29013
- Function did not complete. See previously listed messages for possible cause.
|