Midrange News for the IBM i Community

Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
RPG Report 2012
has no ratings.
Published: 30 Apr 2012
Revised: 30 Sep 2014 - 2920 days ago
Last viewed on: 26 Sep 2022 (5990 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

RPG Report - 01 MAY 2012 Published by: Bob Cozzi on 30 Apr 2012 view comments

© Robert Cozzi, Jr. All rights reserved. Reproduction/Redistribution prohibited.
A midrangeNews.com Publication

Alternate File Names in RPG IV

Using Built-in Support for Declaring the Same File Multiple Times

It wasn't until at least version 5.1 of IBM i that IBM included a feature in RPG IV that I designed and suggested to them long  before RPG IV was ever announced; The EXTFILE and EXTMBR keywords allow the Programmer to specify a runtime file and/or member name. This allows you to avoid using a performance-hogging OVRDBF. In fact, EXTFILE and EXTMBR don't use OVRDBF at all, they simply cause RPG to open the specified file or member directly--no override necessary.


I've written about EXTFILE and EXTMBR before so I won't repeat myself here. To summarize, these File specification keywords accept a literal, named constant or field name that contains the name of the file and/or member to be used at runtime. The file name can be a qualified name using the CL command-style naming convention: library/file

Sponsored by: BCD Software

The member name (if the EXTMBR keyword is specified) must the a valid member name or one of the special values *FIRST, *ALL.

Since these keywords map directly to the internal "record" I/O APIs, the file, library and member names must be in all upper case or will may receive a runtime error.

.....H OPTION(*SRCSTMT:*NODEBUGIO)                                 
      FglMAST    IF   E           K DISK    EXTFILE('APLIB/GLMAST') 
      F                                     PREFIX('F1.')          
      F                                     RENAME(GLREC:glRec1)  
      FglMast2   UF   E           K DISK    EXTFILE('APLIB/GLMAST') 
      F                                     RENAME(GLMREC:glRec2) 
      F                                     PREFIX('F2.')
      D F1              DS                  LikeRec(glRec1) Inz   
      D F2              DS                  LikeRec(glRec2) Inz  

In the example above, I've declared the file GLMAST twice, once as GLMAST and once as GLMAST2. Both files point at APLIB/GLMAST using the EXTFILE keyword. I could have just as easily placed that name into a named constant or a field with an INZ value identifying the file to be used.

The Problem with EXTFILE

Unfortunately, even though the EXTFILE keyword is very useful for utility program where you're reading different files specified at runtime, the problem is the files in positions 7 to 16 of the File specification must exist at compile-time or the program won't compile. So this means at the very minimum, a format file must exist at compile-time, while most programmers tend to create logical files to accommodate this shortcoming.

EXTDESC Saves the Day

Starting with IBM i v6.1 IBM introduced the EXTDESC keyword to supplement the EXTFILE keyword. This keyword allows you to specify the file whose format is used by the compiler. This means we no longer have to create dummy logical files or format files. In fact, it means we don't have to worry about any such thing because the file the compiler uses to pull in the External Description for the file being declared is specified on the EXTDESC keyword--positions 7 to 16 are ignored.

Let's look at my previous example, but this time I include EXTDESC.

.....H OPTION(*SRCSTMT:*NODEBUGIO)                                 
      Fgl1       IF   E           K DISK    EXTDESC('APLIB/GLMAST') 
      F                                     EXTFILE('APLIB/GLMAST') 
      F                                     PREFIX('F1.')          
      F                                     RENAME(GLREC:glRec1)  
      Fgl2       UF   E           K DISK    EXTDESC('APLIB/GLMAST') 
      F                                     EXTFILE('APLIB/GLMAST') 
      F                                     PREFIX('F2.')         
      F                                     RENAME(GLMREC:glRec2) 
      D F1              DS                  LikeRec(glRec1) Inz   
      D F2              DS                  LikeRec(glRec2) Inz  

By adding the EXTDESC keyword I've identified the file that the compiler will use at compile-time to pull in the description of the GL1 and GL2 files (position 7 to 16). I no longer have to specify a "real" file name in positions 7 to 16.

With the introduction of EXTDESC, the EXTFILE keyword was enhanced to support the *EXTDESC option. When EXTFILE(*EXTDESC) is specified, the same file is opened as is pointed to by the the EXTDESC keyword. Unfortunately EXTDESC requires a literal or named constant for its file name so the compile can determine which file to import. That means that if EXTFILE(*EXTDESC) is specified you can't change the file name at runtime. A useful feature if you need it, but except for a few quick-and-dirty programs I really haven't used it too often.

With the introduction of the EXTDESC in v6.2 to complement EXTFILE, we can now write programs that access the same file with multiple open data paths in the same program with very little effort.

Cozzi Tools 2012 - Beta

Cozzi Tools 2012 (which includes the COZTOOLS service program) is scheduled for beta release on May 1, 2012. Watch midrangeNews.com for details, including download links and visit the COZTOOLS website for more information. 

Call Me

Bob Cozzi is a technical advisor to IBM i clients around the world. His specialty is solving difficult problems for his clients, training their programming staffs, and performing system migration/upgrades for small shops. His consulting rates are available online. To contact Bob, send an email to: bob at rpgworld.com

You can subscribe to RPG Report (we call it "follow") by visiting the RPG Report page on midrangeNews.com and then click the FOLLOW link in the table of contents for that page. To unsubscribe, simply click that same link. You must be signed up and signed in to midrangeNews.com to start following RPG Report.

Follow Bob Cozzi on Twitter

Return to midrangenews.com home page.
Sort Ascend | Descend