Saturday, 22 July 2017

Writing records to flat file in RPGLE

While working on AS400 server, users often have to deal with flat files.
Declaring flat file in RPGLE - when a flat file is declared in RPGLE, make sure to rename record format because RPGLE won't allow you to have same file and record format name. In order to rename the record format we use RENAME keyword in F-spec.


01.00 hoption(*nodebugio:*srcstmt)                              
02.00  *                                                        
03.00 Femp       if   e             disk                        
04.00 Fflat      if a e             disk    rename(flat:rflat)  
05.00 F                                     prefix(fld_)        
06.00 F                                     usropn              
07.00  *                                                        
08.00 D command         pr                  extpgm('QCMDEXC')    
09.00 D                              200A   options(*varsize) const
10.00 D                               15P 5 const                
11.00  *                                                        
12.00 D string          s            200A                        
13.00  *                                                        
14.00  /free                                                    
15.00                                                            
16.00     string = 'clrpfm flat';                        
17.00     command (string:%len(%trim(string)));                  
18.00                                                            
19.00     if not %open(flat);                                    
20.00       open flat;                          
21.00     endif;                                
22.00                                          
23.00     read remp;                            
24.00     dow not %eof(emp);                    
25.00       %subst(fld_flat:1:5) = %char(empid);
26.00       %subst(fld_flat:7:27) = empname;    
27.00       %subst(fld_flat:31:60) = empaddrs;  
28.00       write rflat;                        
29.00       read remp;                          
30.00     enddo;                                
31.00                                          
32.00     *inlr = *on;                          
_________________________________________________________________________________

line 8 to 10 - declaring prototype of QCMDEXC program which is used to run CL commands in RPGLE.
line 16 to 17 - clearing flat file. Note that flat file is declared in user open (usropn) mode so that it can be cleared before performing any read/write action, otherwise we will get file already used error.
line 19 to 21 - opening flat file
line 23 - 30 - read emp file and write to flat file. 

No comments:

Post a Comment

Progress bar in AS400 (version 2)

This is another version of progress which most people like (previous version is here ). Source of display file A                     ...