Tips and Techniques: Does Your File Exist on the IFS?

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Many people are using the Integrated File System (IFS) to store source code and HTML pages and to stage converted database files that are being sent or received between heterogeneous systems. Now, with OS/400 V5R2's "Type 2" IFS files, it is actually much less of a performance issue to use the IFS than before.

One question I am frequently asked is how to determine if a file exists on the IFS. There are several methods to accomplish this, but probably the easiest and safest way is to call the access() procedure. The C runtime function name access() allows you to test a file on the IFS for read/write access or existence.

The IFS API manual tells us that the access() procedure is prototyped, in C, as follows:

int access(const char *path, int amode);

This C prototype doesn't do us much good in RPG. We need to convert it to an RPG IV prototype. Illustrated below is the RPG IV prototype for the access() procedure.

     D access          PR            10I 0 ExtProc('access')
     D  szIFSFile                      *   Value OPTIONS(*STRING)
     D  nAccessMode                  10I 0 VALUE

The access() procedure returns 0 if the test succeeds. For example, if you check for file existence and the file exists, 0 is returned.

Of course, to check for the existence of a file, you have to tell the access() procedure what you want to do. To do that, you must specify on the second parameter the type of file access you want. The options for the second parameter are as follows:

     D R_OK            C                   Const(4)
     D W_OK            C                   Const(2)
     D F_OK            C                   Const(0)

It is always better programming to use named constants rather than hard-coded numbers, hence the R_OK, W_OK, and F_OK named constants. If you read the access() documentation, you see that the C language predefines these constants for you (in C language, of course).

Using access()

As mentioned, most people simply want to check to see if a file already exists on the IFS. To do this, you call the access() procedure with the F_OK value specified for its second parameter.

For example, to check to see if the file CUSTMAST.TXT exists in the /MYFILES directory, you could use the following:

     D szIFSfile       C                   '/myfiles/customers.txt'
     C                   if        access(szIFSFile F_OK) = 0
         //   The file exists!!
     C                   endif

In this example, the IFS file name is stored as a named constant (line 1), and on line 2, the access() procedure is called to test for its existence. If it is found, a 0 is returned and processing continues.

As is always the case, to use any of the C runtime library functions, you must include the QC2LE binding directory. As a matter of practice, I always include it in the H-spec for my source code. Here's a typical Header specification from one of my source members:

     H   BNDDIR('QC2LE') OPTIONS(*SRCSTMT:*NODEBUGIO)
      /IF  DEFINED(*CRTBNDRPG)
     H   DFTACTGRP(*NO) 
      /ENDIF

Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.

BLOG COMMENTS POWERED BY DISQUS