Wednesday, July 10, 2013

Useful keywords for your F-specs

Update: The post linked here is updated to use totally free RPG definitions.


I was working within earshot of a colleague who had to recompile several programs he had built, over six months ago, that an user finally wanted to test. The objects had been deleted as part of a software refresh of our development IBM i (AS400), therefore, he would have to compile all of the program objects. He was going through all of the programs’ source code and recreating the work files he needed for the compiles, while grumbling that there must be an easier way to do this. That is when I offered to show him something that would make it easier in the future.

There are several File specification, F-specs, keywords that I use especially with “work” files:

  • EXTFILE
  • EXTDESC
  • EXTMBR
  • RENAME

In this scenario I have created a “work” file in QTEMP, WRK_FILE, that is a copy of the production file, TESTPF. I need to read one of the members, ONE, only, and I need to rename the record format so that it is not the same name as the record format name of the production file.

Below is an example of the code I could write:

1 FTESTPF    IF   E           K DISK
2 FANY_NAME  IF A E           K DISK    extfile('QTEMP/WRK_FILE')
3 F                                       extdesc('TESTPF')
4 F                                       extmbr('ONE')
5 F                                       rename(TESTPFR:WORK_RCD)

Line 2 shows how you can call the file any name you would like. The EXTFILE tells the program where to find the file, the library name is optional. In this case I want it to find the file in QTEMP. This could be considered a replacement for the CL command OVRDBF.

The EXTDESC, on line 3, tells the compiler that it can use the file description of TESTPF when compiling. At compile time QTEMP/WRK_FILE does not have to exist.

One line 4 the EXTMBR is used to denote which member should be read. If this is not given then the first member is read. As most files I deal with have only one member, EXTMBR is rarely used.

The RENAME, line 5, renames the record format of the file. As we have TESTPF in the program this keyword is needed as without it the program will not compile.

In the example below I am passing to this program the file to use and which member to use.

1 FANY_NAME  IF   E           K DISK    extfile(inFile)
2 F                                       extdesc('TESTPF') 
3 F                                       extmbr(inMbr) 
4 F                                       rename(TESTPFR:INPUT)
5 F                                       usropn
6  ************************************************************
7 D inFile          S             21
8 D inMbr           S             10

The file has to be “user opened”, see line 5, as at program initialization the file and member to use are not known. The OPEN operation in the Calculation specifications, C-spec, will do this.

I made the field inFile 21 long, see line 7, so it can be big enough to contain the largest library name, slash character, and file name.

More information about these keywords can be found on the IBM website...

 

This article was written for IBM i 7.1, and it should work with earlier releases too.

14 comments:

  1. Thanks Simon. This will be helpful.

    ReplyDelete
  2. I've use almost all the keywords in earlier projects. The examples are great and worth a reading. Thought I'm not into development now, this post reminds me of the valuable keywords. Thanks for the post.

    ReplyDelete
  3. I still maintain legacy RPG programs and the F-Spec will always stay. Also thanks for the reminder.

    ReplyDelete
  4. Well done, Simon. Thanks for the reminder. F specs are not going away.

    ReplyDelete
  5. I'm excited by the newer capabilities of qualified files, locally declared files, passing files as parameters etc.

    ReplyDelete
  6. Just what I needed to valadate container file in another Company Library,

    Thank you,

    ReplyDelete
  7. Hi Simon,could you please reply to below queries

    1.file ending in cl
    2.how to read member in rpg
    3.one physival having 2 members in
    4.how to open files in rpg implicitly -
    5.how to handle errors in rpg - infds ppsr
    6.how to use particular member in rpg n how we read tht in rpg witout using EXTMBR and without using OVRDBF

    ReplyDelete
    Replies
    1. 1. Do you mean how to monitor when you reach the end of file you are reading in CL? If so insert a MONMSG after the RCVF.

      2. Read the post this comment is on.

      3. Do not understand what you mean.

      4. OPEN operation code. You will find more information at the link to the OS i 7.1 manuals on this page. Go to it & search for "RPG OPEN file". Don't be afraid to use the IBM manuals, that is why I put a link to them on this blog.

      5. I don't use the PSSR. I use the info from the Program Status Data Structure. For an example see: http://www.rpgpgm.com/2013/10/capturing-qcmdexc-error-codes.html

      6. If there is more than one member & you do not use the EXTMBR or OVRDBF you will only read the first member.

      Delete
  8. Can i mention psds in F specf

    ReplyDelete
  9. thanks for introducing me to extdesc - J

    ReplyDelete
  10. Hi Simon, you are doing great Work. Just a query can we read two members of same file in a single RPGLE program. I tried F-spec like below but dint work out. Can you please advise.
    FMMFILE IF E K disk Extfile(FMFILE)
    F extmbr('MMFILE')
    F Rename(ZMFFMT : FFILEFMT)
    F Prefix(A_)
    FMMFILE IF E K disk Extfile(SMFILE)
    F extmbr('SMMFILE')
    F Rename(ZMFFMT : SFILEFMT)
    F Prefix(B_)

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.