Open a file in rpgle using character field for file name

Posted on 2009-04-28
Medium Priority
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
  • 2
LVL 36

Expert Comment

by:Gary Patterson
ID: 24252985
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


Author Comment

ID: 24253817
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 36

Accepted Solution

Gary Patterson earned 500 total points
ID: 24256948
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

ID: 24337447
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


Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below errors for MS Exchange Server 2016 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

829 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