?
Solved

Open a file in rpgle using character field for file name

Posted on 2009-04-28
5
Medium Priority
?
2,078 Views
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)
  OPEN TESTFILE;
but is there a way to have character field in place of the filename:
  D filename           S             10
/free
    filename = 'TESTFILE';
    open filename;    
0
Comment
Question by:joeschuh
  • 2
4 Comments
 
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')                
                                                                
 /free                                                          
   cmd = 'OVRDBF FILE(DUMMYFILE) TOFILE(' + %trim(file) + ') ' +
                'OVRSCOPE(*JOB)';                               
  executeCmd( cmd : %len(cmd) );
  open dummyFile;    // Opens PF1                            
  *INLR = *ON;                                                  
 /end-free        
 
 
*******************************************
* Extfile example
*******************************************
 
fdummyFile if   e             disk    extfile(file) 
                                                    
d file            s             10a                 
                                                    
 /free                                              
   file = 'PF1';                                    
   open dummyFile;                                  
   return                                           
 /end-free                                                                                        

Open in new window

0
 
LVL 2

Author Comment

by:joeschuh
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.

thaks
     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
 
     dfile_Control...
     d                 pr              n
     d function                      10    const
     d filenameList                 500    const
 
      /free
          file_control('OPEN':'file1,file2');
 
              //read stuff from the files
 
          file_control('CLOSE':'file1');
 
             // read some more stuff from file2
 
          file_control('CLOSE':'file2');
 
 
          *inlr = '1';
          return;
      /end-free
 
 
     pfile_control...
     p                 b
     dfile_control...
     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
 
      /free
         monitor;
           // 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' ;
               select;
                 when filename = 'file1';
                   if %open(file1);
                     close file1;
                   endif;
                 when filename = 'file2';
                   if %open(file2);
                     close file2;
                   endif;
               endsl;
             else;
               select;
                 when filename = 'file1';
                   if not %open(file1);
                     open file1;
                   endif;
                 when filename = 'file2';
                   if not %open(file2);
                     open file2;
                   endif;
               endsl;
             endif;
             endfor;
 
           on-error;
             return '1';
           endmon;
 
           return '0';
      /end-free
     pfile_control...
     p                 e                                 

Open in new window

0
 
LVL 36

Accepted Solution

by:
Gary Patterson earned 500 total points
ID: 24256948
Use the C-language API _Ropen:

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/books/sc415607.pdf#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:

http://www.scandevconf.se/db/What-you-can-do-with-RPG.pdf

- Gary Patterson


0
 

Expert Comment

by:quadrucle
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                                 
                                                                          
 /Free                                                                    
                                                                          
  FileName = 'BBB';                                                       
  open AAA;                                                               
  Read AAA;                                                               
  RTRQID = RTRQID;                                                        
                                                                          
  *inlr = '1';                                                            
                                                                          
 /End-Free                                                                

Open in new window

0

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