Open a file in rpgle using character field for file name

Posted on 2009-04-28
Last Modified: 2012-05-06
Is there a way in an rpg program to open a file using a character field for the filename?
for example: if my file name is TESTFILE I can use the line: (in rpg free)
but is there a way to have character field in place of the filename:
  D filename           S             10
    filename = 'TESTFILE';
    open filename;    
Question by:joeschuh
    LVL 34

    Expert Comment

    by:Gary Patterson
    Sure,  you have a couple of options:

    1) Use the CL command OVRDBF to override the file to the file name in the variable.  (See code below).  Wwhen you compile the DUMMYFILE either needs to exist and have the same format as the file that you are overriding to, or you need to have an override to a valid file in place in your compile job.  See examples below.

    2) Use the EXTFILE keyword on the file spec, using a variable name.  See ecamples below.

    3) Use embedded SQL.

    - Gary Patterson

    * OVRDBF example
    fdummyFile if   e             disk    usropn                          
    d executeCmd      pr                  extpgm( 'QCMDEXC' )       
    d  command                     999a   const                     
    d  size                         15p 5 const                     
    d cmd             s            999a                             
    d file            s             10a   inz('PF1')                
       cmd = 'OVRDBF FILE(DUMMYFILE) TOFILE(' + %trim(file) + ') ' +
      executeCmd( cmd : %len(cmd) );
      open dummyFile;    // Opens PF1                            
      *INLR = *ON;                                                  
    * Extfile example
    fdummyFile if   e             disk    extfile(file) 
    d file            s             10a                 
       file = 'PF1';                                    
       open dummyFile;                                  

    Open in new window

    LVL 2

    Author Comment

    What I want to be able to do is define several files as usropn in the f specs and then create a procedure I can call to open 1 or more of the files when needed. This is for service programs where I want to be able to define every file as usropn and then open and close them as needed.
    I am currently doing it with a big select statement as in the example below. However, some modules require a large number of files so the select statement will get a bit long.

         ffile1     if   e           k disk    usropn
         ffile2     if   e           k disk    usropn
          * file control opens or closees the specified files
          * parms:  function      OPEN or CLOSE
          *         filenamelist  a comma seperated list of files to open or close
         d                 pr              n
         d function                      10    const
         d filenameList                 500    const
                  //read stuff from the files
                 // read some more stuff from file2
              *inlr = '1';
         p                 b
         d                 pi              n
         d function                      10    const
         d filenameList                 500    const
         d filenames       s             10    dim(10)
         d fileCount       s              2  0
         d idx             s              2  0
         d filename        s             10
               // I have a procedure here that splits the incoming string into an array
               //  and puts the total count in fileCount
               //filenames = array of filenames
               // run through the filenames in the array and open the files
               for idx = 1 to fileCount;
               filename =  filenames(idx) ;
                 if function = 'CLOSE' ;
                     when filename = 'file1';
                       if %open(file1);
                         close file1;
                     when filename = 'file2';
                       if %open(file2);
                         close file2;
                     when filename = 'file1';
                       if not %open(file1);
                         open file1;
                     when filename = 'file2';
                       if not %open(file2);
                         open file2;
                 return '1';
               return '0';
         p                 e                                 

    Open in new window

    LVL 34

    Accepted Solution

    Use the C-language API _Ropen:

    You can either just write a little ILE C / C++ module and use it, or you can use the C API directly in your ILE RPG program:

    - Gary Patterson


    Expert Comment

    You can use the ExtFile with variable name in it to do this easily. Please remember that all the files you use needs to be in the same format.
    fAAA       if   e           k disk    rename(AAA: AAAR) usropn            
    f                                     Extfile(FileName)                   
    d OPNFILE         pr                                                      
    d OPNFILE         pi                                                      
    d FileName        s              3a   inz                                 
      FileName = 'BBB';                                                       
      open AAA;                                                               
      Read AAA;                                                               
      RTRQID = RTRQID;                                                        
      *inlr = '1';                                                            

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    For Sennheiser, comfort, quality and security are high priority areas. This paper addresses the security of Bluetooth technology and the supplementary security that Sennheiser’s Contact Center and Office (CC&O) headsets provide.  
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now