Sign in

docs RPG Reference

Rules for the LIKEFILE keyword:

Rules for the LIKEFILE keyword:

  • When a file is defined with the LIKEFILE keyword, the QUALIFIED keyword is assumed. Record formats are automatically qualified for a file defined with the LIKEFILE keyword. If the record formats of the parent file FILE1 are RECA and RECB, then the record formats of the new file FILE2 must be referred to in the RPG program by FILE2.RECA and FILE2.RECB.
  • The QUALIFIED keyword cannot be specified with the LIKEFILE keyword.
  • All non-ignored record formats from the parent file are available for the new file.
  • If the LIKEFILE keyword is specified, the file specified as a parameter must have already been defined in the source file.
  • If the LIKEFILE keyword is specified in a subprocedure, and the file specified as the parameter is defined in the global definitions, the compiler will locate the global definition at the time of scanning the LIKEFILE definition.
  • Input and output specifications are not generated or allowed for files defined with LIKEFILE. All input and output operations must be done with result data structures.
  • When a file is defined with LIKEFILE, the File specifications for the parent file must make it clear whether or not the file is blocked. It may be necessary to specify the BLOCK keyword for the parent file. For example, for an input DISK file, the BLOCK keyword is required if the file is used in a LIKEFILE keyword since the file is blocked depending on which calculation operations are used for the file. For an Input-Add DISK file, the file can never be blocked, so the BLOCK keyword is not required.
  • If BLOCK(*YES) is specified for a file, and the file is used as a parent file for files defined with the LIKEFILE keyword, the READE, READPE and READP operations are not allowed for the parent file, or for any files related to the parent file through the LIKEFILE keyword.
  • Some properties of the parent file are inherited by the new file, and some are not. Of the properties which are inherited, some can be overridden by File specification keywords. The properties which are not inherited can be specified for the new file by File specification keywords, see Table 1.

Table 1. File properties which are inherited and which can be overridden

Property or keywordInherited from parent fileCan be specified for new file
File type (Input, update, output, combined)YesNo
File additionYesNo
Record address type (RRN, keyed)YesNo
Record length (Program-described files)YesNo
Key length (Program-described files)YesNo
File organization (Program-described files)YesNo
DeviceYesNo
ALIASYesNo
BLOCKYesNo
CHARCOUNTYesNo
COMMITNoYes
DATFMTN/A, see Note 1
DEVIDNoYes
DISKYesNo
EXTDESCYesNo
EXTFILEYes, see Note 2Yes
EXTINDNoYes
EXTMBRYes, see Note 2Yes
FORMLENYesYes
FORMOFLYesYes
HANDLERN/A, the HANDLER keyword is not supported for either the new file or the parent file.
IGNOREYesNo
INCLUDEYesNo
INDDSNoYes
INFDSNoYes
INFSRNoYes
KEYEDYesNo
KEYLOCYesNo
LIKEFILEYesN/A
MAXDEVYesYes
OFLINDNoYes
PASSYesNo
PGMNAMEYesYes
PLISTNoYes
PREFIXYesNo
PRINTERYesNo
PRTCTLNoYes
QUALIFIEDN/A, QUALIFIED is always implied for new file
RAFDATAN/A, see Note 3
RECNONoYes
RENAMEYesNo
SAVEDSNoYes
SAVEINDNoYes
SEQYesNo
SFILEYes, see Note 4Yes, see Note 4
SLNNoYes
SPECIALYesNo
STATICNoYes
TEMPLATENoYes
TIMFMTN/A, see Note 1
USAGEYesNo
USROPNNoYes
WORKSTNYesNo

Note:

  1. The DATFMT and TIMFMT keywords relate to Date and Time fields coded on program-described Input specifications for the file, but Input specifications are not relevant for files defined with the LIKEFILE keyword.

  2. The external file associated with the RPG file depends on the EXTFILE and EXTMBR keywords specified for both the parent file and the new file. By default, the external file associated with each file is the name specified in the Name entry for the file. The new file inherits the EXTFILE or EXTMBR keywords from the parent file if the parameters are constants, but these keywords may also be specified for the new file. If the parameter for EXTFILE or EXTMBR is not a constant, the EXTFILE or EXTMBR keyword is not inherited. The following table shows the external files that would be used at runtime for some examples of EXTFILE and EXTMBR values for a parent file and a new file that is defined LIKEFILE the parent file.

    Table 2. File specification examples: EXTFILE and EXTMBR

    File SpecificationsExternal files used at runtime (Inherited values appear in bold)
    Examples where the EXTFILE and EXTMBR values are both constants
    rpgle FFILE1 IF E DISK FFILE2 LIKEFILE(FILE1) rpgle *LIBL/FILE1(*FIRST) *LIBL/FILE2(*FIRST)
    rpgle FFILE1 IF E DISK EXTFILE('MYLIB/MYFILE') FFILE2 LIKEFILE(FILE1) rpgle MYLIB/MYFILE(*FIRST) MYLIB/MYFILE(*FIRST)
    rpgle FFILE1 IF E DISK FFILE2 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE') rpgle *LIBL/FILE1(*FIRST) MYLIB/MYFILE(*FIRST)
    rpgle FFILE1 IF E DISK EXTFILE('MYLIB/MYFILE1') FFILE2 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE2') rpgle MYLIB/MYFILE1(*FIRST) MYLIB/MYFILE2(*FIRST)
    rpgle FFILE1 IF E DISK EXTMBR('MBR1') FFILE2 LIKEFILE(FILE1) rpgle *LIBL/FILE1(MBR1) *LIBL/FILE2(MBR1)
    rpgle FFILE1 IF E DISK FFILE2 LIKEFILE(FILE1) EXTMBR('MBR1') rpgle *LIBL/FILE1(*FIRST) *LIBL/FILE2(MBR1)
    rpgle FFILE1 IF E DISK EXTMBR('MBR1') FFILE2 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE2') rpgle *LIBL/FILE1(MBR1) MYLIB/MYFILE2(MBR1)
    Examples where the EXTFILE and EXTMBR values are both variable
    rpgle FFILE1 IF E DISK EXTFILE(extfileVariable) FFILE2 LIKEFILE(FILE1) Value of extfileVariable: 'MYLIB/MYFILE' rpgle MYLIB/MYFILE(*FIRST) *LIBL/FILE2(*FIRST)
    rpgle FFILE1 IF E DISK FFILE2 LIKEFILE(FILE1) EXTFILE(extfileVariable) Value of extfileVariable: 'MYLIB/MYFILE' rpgle *LIBL/FILE1(*FIRST) MYLIB/MYFILE(*FIRST)
    rpgle FFILE1 IF E DISK EXTFILE(extfileVariable1) FFILE2 LIKEFILE(FILE1) EXTFILE(extfileVariable2) Value of extfileVariable1: 'MYLIB/MYFILE1' Value of extfileVariable2: 'MYLIB/MYFILE2' rpgle MYLIB/MYFILE1(*FIRST) MYLIB/MYFILE2(*FIRST)
    rpgle FFILE1 IF E DISK EXTMBR(extmbrVariable) FFILE2 LIKEFILE(FILE1) Value of extmbrVariable: 'MBR1' rpgle *LIBL/FILE1(MBR1) *LIBL/FILE2(*FIRST)
    rpgle FFILE1 IF E DISK FFILE2 LIKEFILE(FILE1) EXTMBR(extmbrVariable) Value of extmbrVariable: 'MBR1' rpgle *LIBL/FILE1(*FIRST) *LIBL/FILE2(MBR1)
    rpgle FFILE1 IF E DISK EXTMBR(extmbrVariable) FFILE2 LIKEFILE(FILE1) EXTFILE(extfileVariable) Value of extmbrVariable: 'MBR1' Value of extfileVariable: 'MYLIB/MYFILE2' rpgle *LIBL/FILE1(MBR1) MYLIB/MYFILE2(*FIRST)
    Examples where the EXTFILE and EXTMBR values are mixed variables and constants
    rpgle FFILE1 IF E DISK EXTFILE(extfileVariable1) EXTMBR('MBR1') FFILE2 LIKEFILE(FILE1) Value of extfileVariable1: 'MYLIB/MYFILE1' rpgle MYLIB/MYFILE1(MBR1) *LIBL/FILE2(MBR1)
    rpgle FFILE1 IF E DISK EXTMBR(extmbrVariable) FFILE2 LIKEFILE(FILE1) Value of extmbrVariable: 'MBR1' rpgle *LIBL/FILE1(MBR1) *LIBL/FILE2(*FIRST)
    rpgle FFILE1 IF E DISK EXTFILE('MYLIB/MYFILE1') EXTMBR(extmbrVariable) FFILE2 LIKEFILE(FILE1) Value of extmbrVariable: 'MBR1' rpgle MYLIB/MYFILE1(MBR1) MYLIB/MYFILE1(*FIRST)
  3. The RAFDATA keyword is relevant only for Primary and Secondary files, but the parent file must be a Full Procedural file.

  4. The SFILE keyword indicates that the record format is a subfile record format, and it also indicates the name of the variable used to specify the relative record number for the subfile. The new file automatically inherits the fact that a particular record format is a subfile record format; however, it does not inherit the name of the variable used to specify the RRN. The SFILE keyword must be specified for the new file to indicate which variable is to be used to specify the relative record number for the subfile.