Blog Archive

RPGLE write data to IFS - Using C APIs open() close() Write()

h dftactgrp(*no) bnddir('QC2LE')              
h option (*nodebugio)                         
                                              
d/copy *libl/qrpglesrc,IFSIO_H                
                                             
d fd              s             10I 0         
d crlf            c                   x'0D25' 
d $xml            s            512a           
                                              
d $file_name      s             50            
d $file_path      s            100            
                                              
d ProgStatus     sds                          
d  Parms            *PARMS                    
d  ProgName         *PROC                     
d  ErrMsgID              40     46            
d  ErrMsg                91    169            
d  JobName              244    253            
d  Userid               254    263            
d  JobNumber            264    269            
                                              
 *                                            
 *----- Main Routine                           
*                                                          
c                   exsr      @open_file                    
c                   exsr      @write_data                   
c                   exsr      @close_file                   
c                   eval      *inlr = *on                   
c                   return                                  
 *                                                          
 *----- Open IFS file                                       
 *                                                          
c     @open_file    begsr                                   
                                                            
c                   eval      $file_name = 'Mydata.xml'     
c                   eval      $file_path = '/Myxml/data/' + 
c                                          %trim($file_name)
 /free                                                      
                                                            
   fd = open(%trim($file_path)                              
             : O_WRONLY+O_CREAT+O_TRUNC+O_CCSID             
             : S_IRGRP + S_IWGRP + S_IXGRP                  
             : 819);                                        
   callp close(fd);                                         
   fd = open(%trim($file_path):O_WRONLY+O_TEXTDATA);        
                                                            
   $xml = '<?xml version="1.0" encoding="UTF-8"?>' + crlf +
          '<MyData>' + crlf;                               
   callp write(fd: %addr($xml): %len(%trim($xml)));        
                                                           
 /end-free                                                 
                                                           
c                   endsr                                  
 *                                                         
 *----- Close IFS file                                     
 *                                                         
c     @close_file   begsr                                  
                                                           
 /free                                                     
                                                           
   $xml = '</MyData>' + crlf;                              
   callp write(fd: %addr($xml): %len(%trim($xml)));        
   callp close(fd);                                        
                                                           
 /end-free                                                 
                                                           
c                   endsr                                  
 *                                                         
 *----- Write Data into the IFS file                       
*                                                    
c     @write_data   begsr                             
                                                      
 /free                                                
                                                      
   $xml = '<Name>' +                                  
            'My name is $user_id' +                   
          '</Name>' + crlf;                           
   callp write(fd: %addr($xml): %len(%trim($xml)));   
                                                      
   $xml = '<Date>' +                                  
            'Today is ' + %char(%date()) +            
          '</Date>' + crlf;                           
   callp write(fd: %addr($xml): %len(%trim($xml)));   
                                                      
 /end-free                                            
                                                      
c                   endsr                             
 *                                                    
 *----- Initial Routine                               
 *                                                    
c     *inzsr        begsr                             
                          
c                   endsr     

Output for the above program in IFS

<?xml version="1.0" encoding="UTF-8"?>     
<MyData>                                   
<Name>My name is $user_id</Name>           
<Date>Today is 2011-06-08</Date>           
</MyData>         

Click here if you need source for IFSIO_H