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 keyword Inherited from parent file Can be specified for new file
File type (Input, update, output, combined) Yes No
File addition Yes No
Record address type (RRN, keyed) Yes No
Record length (Program-described files) Yes No
Key length (Program-described files) Yes No
File organization (Program-described files) Yes No
Device Yes No
ALIAS Yes No
BLOCK Yes No
COMMIT No Yes
DATFMT N/A, see Note 1
DEVID No Yes
DISK Yes No
EXTDESC Yes No
EXTFILE Yes, see Note 2 Yes
EXTIND No Yes
EXTMBR Yes, see Note 2 Yes
FORMLEN Yes Yes
FORMOFL Yes Yes
HANDLER N/A, the HANDLER keyword is not supported for either the new file or the parent file.
IGNORE Yes No
INCLUDE Yes No
INDDS No Yes
INFDS No Yes
INFSR No Yes
KEYED Yes No
KEYLOC Yes No
LIKEFILE Yes N/A
MAXDEV Yes Yes
OFLIND No Yes
PASS Yes No
PGMNAME Yes Yes
PLIST No Yes
PREFIX Yes No
PRINTER Yes No
PRTCTL No Yes
QUALIFIED N/A, QUALIFIED is always implied for new file
RAFDATA N/A, see Note 3
RECNO No Yes
RENAME Yes No
SAVEDS No Yes
SAVEIND No Yes
SEQ Yes No
SFILE Yes, see Note 4 Yes, see Note 4
SLN No Yes
SPECIAL Yes No
STATIC No Yes
TEMPLATE No Yes
TIMFMT N/A, see Note 1
USAGE Yes No
USROPN No Yes
WORKSTN Yes No
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 Specifications External files used at runtime (Inherited values appear in bold)
    Examples where the EXTFILE and EXTMBR values are both constants
    FFILE1  IF  E    DISK
    FFILE2                 LIKEFILE(FILE1)
    *LIBL/FILE1(*FIRST)
    *LIBL/FILE2(*FIRST)
    FFILE1  IF  E    DISK  EXTFILE('MYLIB/MYFILE')
    FFILE2                 LIKEFILE(FILE1)
    MYLIB/MYFILE(*FIRST)
    MYLIB/MYFILE(*FIRST)
    FFILE1  IF  E    DISK
    FFILE2                 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE')
    *LIBL/FILE1(*FIRST)
    MYLIB/MYFILE(*FIRST)
    FFILE1  IF  E    DISK  EXTFILE('MYLIB/MYFILE1')
    FFILE2                 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE2')
    MYLIB/MYFILE1(*FIRST)
    MYLIB/MYFILE2(*FIRST)
    FFILE1  IF  E    DISK  EXTMBR('MBR1')
    FFILE2                 LIKEFILE(FILE1)
    *LIBL/FILE1(MBR1)
    *LIBL/FILE2(MBR1)
    FFILE1  IF  E    DISK
    FFILE2                 LIKEFILE(FILE1) EXTMBR('MBR1')
    *LIBL/FILE1(*FIRST)
    *LIBL/FILE2(MBR1)
    FFILE1  IF  E    DISK  EXTMBR('MBR1')
    FFILE2                 LIKEFILE(FILE1) EXTFILE('MYLIB/MYFILE2')
    *LIBL/FILE1(MBR1)
    MYLIB/MYFILE2(MBR1)
    Examples where the EXTFILE and EXTMBR values are both variable
    FFILE1  IF  E    DISK  EXTFILE(extfileVariable)
    FFILE2                 LIKEFILE(FILE1)
    Value of extfileVariable: 'MYLIB/MYFILE'
    MYLIB/MYFILE(*FIRST)
    *LIBL/FILE2(*FIRST)
    FFILE1  IF  E    DISK
    FFILE2                 LIKEFILE(FILE1) EXTFILE(extfileVariable)
    Value of extfileVariable: 'MYLIB/MYFILE'
    *LIBL/FILE1(*FIRST)
    MYLIB/MYFILE(*FIRST)
    FFILE1  IF  E    DISK  EXTFILE(extfileVariable1)
    FFILE2                 LIKEFILE(FILE1) EXTFILE(extfileVariable2)
    Value of extfileVariable1: 'MYLIB/MYFILE1'
    Value of extfileVariable2: 'MYLIB/MYFILE2'
    MYLIB/MYFILE1(*FIRST)
    MYLIB/MYFILE2(*FIRST)
    FFILE1  IF  E    DISK  EXTMBR(extmbrVariable)
    FFILE2                 LIKEFILE(FILE1)
    Value of extmbrVariable: 'MBR1'
    *LIBL/FILE1(MBR1)
    *LIBL/FILE2(*FIRST)
    FFILE1  IF  E    DISK
    FFILE2                 LIKEFILE(FILE1) EXTMBR(extmbrVariable)
    Value of extmbrVariable: 'MBR1'
    *LIBL/FILE1(*FIRST)
    *LIBL/FILE2(MBR1)
    FFILE1  IF  E    DISK  EXTMBR(extmbrVariable)
    FFILE2                 LIKEFILE(FILE1) EXTFILE(extfileVariable)
    Value of extmbrVariable: 'MBR1'
    Value of extfileVariable: 'MYLIB/MYFILE2'
    *LIBL/FILE1(MBR1)
    MYLIB/MYFILE2(*FIRST)
    Examples where the EXTFILE and EXTMBR values are mixed variables and constants
    FFILE1  IF  E    DISK  EXTFILE(extfileVariable1) EXTMBR('MBR1')
    FFILE2                 LIKEFILE(FILE1)
    Value of extfileVariable1: 'MYLIB/MYFILE1'
    MYLIB/MYFILE1(MBR1)
    *LIBL/FILE2(MBR1)
    FFILE1  IF  E    DISK  EXTMBR(extmbrVariable)
    FFILE2                 LIKEFILE(FILE1)
    Value of extmbrVariable: 'MBR1'
    *LIBL/FILE1(MBR1)
    *LIBL/FILE2(*FIRST)
    FFILE1  IF  E    DISK  EXTFILE('MYLIB/MYFILE1') EXTMBR(extmbrVariable)
    FFILE2                 LIKEFILE(FILE1)
    Value of extmbrVariable: 'MBR1'
    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.