Examples to process flat files in ILE RPG (RPG IV)

As discussed on the previous page, processing a flat file in RPG depends upon how the physical file has been created. Due to this reason you will find two different examples below.

Example 1: When flat file is created using the CRTPF command, Without DDS.
Part 1. Write to the flat file using RPG IV.

** A flat file is defined as any other ordinary Physical file.   
FFlatfile  IF A E             Disk                                
 ** Rename the record format name                                 
F                                     Rename(Flatfile: FLATFILE01)
D @FirstName      S             10                                
D @LastName       S             10                                
D @Sex            S              6                                
D @Age            S              2  0                             
D W@FlatFile      DS                  Inz                         
DFirstName                1     10                                
DLastName                11     20                                
DSex                     21     26                                
DAge                     27     28  0                             
 **                                                               
IFLATFILE01                                                           
 ** Associate the database field with data structure.                 
I..............Ext-field+..................Field+++++++++L1M1..PlMnZr.
I              FLATFILE                    W@FlatFile                 
 ** Everything set now. Use the flat flat as a physical file with four
 ** columns namely FirstName, LastName, Sex and Age!                  
 ** Write data to this flat file as you would do with any other PF.   
C     *Entry        PList                                             
C                   Parm                    @FirstName                
C                   Parm                    @LastName                 
C                   Parm                    @Sex                      
C                   Parm                    @Age                      
 ** Populate file fields                                              
C                   Eval      FirstName = @FirstName                  
C                   Eval      LastName = @LastName                    
C                   Eval      Sex = @Sex                              
C                   Eval      Age = @Age                              
** Write to the flat file                                   
C                   Write     FLATFILE01                     
C                   Eval      *InLr = *On                    
C                   Return                                   

Called this program two times with following commands.

call wrtflatfil parm('Maria' 'Sharapova' 'Female' X'023F')

call wrtflatfil parm('Brad' 'Pitt' 'Male' X'044F')

Output in the flat file.


Line   ....+....1....+....2....+...     
       FLATFILE                         
000001 Maria     Sharapova Female23     
000002 Brad      Pitt      Male  44     
****** ********  End of report  ********

Part 2:- Read a flat file using RPG IV

FFlatfile  IF A E             Disk                                    
 ** Rename the record format name                                     
F                                     Rename(Flatfile: FLATFILE01)    
D W@FlatFile      DS                                                  
DFirstName                1     10                                    
DLastName                11     20                                    
DSex                     21     26                                    
DAge                     27     28  0                                 
 **                                                                   
IFLATFILE01                                                           
 ** Associate the database field with data structure.                 
I              FLATFILE                    W@FlatFile                 
 ** Everything set now. Use the flat flat as a physical file with four
 ** columns namely FirstName, LastName, Sex and Age!                  
 ** Read data from this flat file as you would do with any other PF.  
C                   Read      FLATFILE01                              
C                   DoW       Not %EoF(FLATFILE)             
C     FirstName     Dsply                                    
C     LastName      Dsply                                    
C     Sex           Dsply                                    
C     Age           Dsply                                    
C                   Read      FLATFILE01                     
C                   EndDo                                    
C                   Eval      *InLr = *On                    
C                   Return                                   

Output of sample run of the program.

DSPLY  Maria     
DSPLY  Sharapova 
DSPLY  Female    
DSPLY  23        
DSPLY  Brad      
DSPLY  Pitt      
DSPLY  Male      
DSPLY  44        

You must be happy to see your data separately, ain't you?


Example 2: When flat file has been created from a DDS.
As explained earlier, this time we do not need the I-Spec renaming. Instead we can just write an Eval statement which populates the data structure.

The source code of the example which reads such a flat file has been given below.

FFlatfile  IF A E             Disk                  
D W@FlatFile      DS                                
DFirstName                1     10                  
DLastName                11     20                  
DSex                     21     26                  
DAge                     27     28  0               
 ** Read the flat file                              
C                   Read      FLATFILE01            
C                   Eval      W@FlatFile = FLATRCD  
C     FirstName     DSPLY                           
C     LastName      DSPLY                           
C     Sex           DSPLY                           
C     Age           DSPLY                           
C                   Eval      *InLr = *On           
C                   Return                          

Notice that we do not need any renaming here. In this example, the attributes of the flat file are as below.

  1. Name of the flat file object – FLATFILE
  2. Name of the record format – FLATFILE01
  3. Name of the DDS field – FLATRCD.

The above example illustrates only how to read such a flat file. However, you can yourself modify this source according to your need to write, delete or update any specific record of this flat file.

With this example, I end this long article (published in three days!) now. Your valuable comments and/ or feedback are most welcome.