Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Append data to a IFS stream file in RPGLE

Append data to a IFS stream file in RPGLE.
Append data to a IFS stream file in RPGLE, open(), write(), close(), SQLRPGLE , ifs , RPGLE, write data into the IFS file, Closes the file, open(), write(),  close(),  c apis, as400, ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial, working with ifs, integrated file system,UNIX-type APIs,C language prototype of read() ,extproc,Working with the IFS in RPG IV, prototyping of read() api,The path parameter,The oflag parameter,The mode parameter, The codepage parameter,Introduction to the IFS,
Append data to a IFS stream file in RPGLE

In this article we will discuss how to append data to a ifs stream file i.e. we will start writing the data after the last byte has been written to the existing ifs stream file. Therefore, we will open the ifs file using the specific append flags with the ifs open() C api and will use normal write() api to write/append the data into the existing ifs stream file.

RPGLE program for Append data to a IFS stream file

RPG Code in Fixed format to Append data to a IFS stream file in RPGLE.
     HDFTACTGRP(*NO)                                                                                
                                                                                                    
     D close           PR            10i 0 extproc('close')                     *                   
     D fileds                        10i 0 value                                *file descriptor    
                                                                                                    
     D write           PR            10i 0 extproc('write')                     * 32 bit, no. of byt
     D fileds                        10i 0 value                                *file descriptor    
     D buffer                          *   value                                * pointer to byte   
     D noofbytes                     10U 0 value                                * 32 bits           
                                                                                                    
     D open            PR            10I 0 extproc('open')                                          
     D ifspath                         *   value options(*string)               *ifs path           
     D oflag                         10I 0 value                                *string of 32 bits  
     D mode                          10U 0 value options(*nopass)               * 9 bits            
     D codepage                      10U 0 value options(*nopass)               *                   
                                                                                                    
      * <-----oflag---->                                                                            
     D O_readonly      C                   1                                                        
     D O_writeonly     C                   2                                                        
     D O_readwrite     C                   4                        
     D O_createfileifnotexist...                                    
     D                 C                   8                        
     D O_exclusivecreate...                                         
     D                 C                   16                       
     D O_truncateto0bytes...                                        
     D                 C                   64                       
     D O_appendtofile  C                   256                      
     D O_converttextbycodepage...                                   
     D                 C                   8388608                  
     D O_openintextmode...                                          
     D                 C                   16777216                 
      *                                                             
      * <-----mode---->                                            
      * owner,group,other (RWX)                                     
      *                                         owner authority     
     D M_readowner     C                   256                      
     D M_writeowner    C                   128                      
     D M_executeowner  C                   64                       
      *                                         group authority     
     D M_readgroup     C                   32                         
     D M_writegroup    C                   16                         
     D M_executegroup  C                   8                          
      *                                         other people          
     D M_readother     C                   4                          
     D M_writeother    C                   2                          
     D M_executeother  C                   1                          
      *                                                               
     Difspath          s            512a                              
     Doflag            s             10I 0                            
     Dmode             s             10U 0                            
     Dcodepage         s             10U 0                            
     Dfiledescriptor   s             10i 0                            
     Difsdata          s             11a   inz                        
     Dreturn_write     s             10i 0 inz                        
     Dreturn_close     s             10i 0 inz                        
                                                                      
     C                   EVAL      ifspath = '/home/easyclass/append' 
     C                   EVAL      oflag = O_writeonly +              
     C                                     O_createfileifnotexist +   
     C                                     O_appendtofile                  
     C                   EVAL      mode = M_readowner +                    
     C                                    M_writeowner +                   
     C                                    M_executeowner                   
                                                                           
     C                   EVAL      filedescriptor = open(%trim(ifspath):   
     C                                                   oflag:            
     C                                                   mode)             
     C                   IF        filedescriptor < 0                      
     C                   RETURN                                            
     C                   ENDIF                                             
                                                                           
     C                   EVAL      ifsdata = 'DATA APPEND'                 
     C                   EVAL      return_write = write(filedescriptor:    
     C                                   %addr(ifsdata):%size(ifsdata))    
     C                   IF        return_write < %size(ifsdata)           
     C                   RETURN                                            
     C                   ENDIF                                             
                                                                           
     C                   EVAL      ifsdata = 'CONTINUED'                   
     C                   EVAL      return_write = write(filedescriptor:   
     C                                   %addr(ifsdata):%size(ifsdata))   
     C                   IF        return_write < %size(ifsdata)          
     C                   RETURN                                           
     C                   ENDIF                                            
                                                                          
     C                   EVAL      return_close = close(filedescriptor)   
     C                   IF        return_close = -1                      
     C                   RETURN                                           
     C                   ENDIF                                            
                                                                          
     C                   EVAL      *INLR = *ON                            
     C                   RETURN                                                                                    
RPG Code in /Free and /End-Free format to Append data to a IFS stream file in RPGLE.
     HDFTACTGRP(*NO)                                                                                 
                                                                                                     
     D close           PR            10i 0 extproc('close')                     *                    
     D fileds                        10i 0 value                                *file descriptor     
                                                                                                     
     D write           PR            10i 0 extproc('write')                     * 32 bit, no. of byt 
     D fileds                        10i 0 value                                *file descriptor     
     D buffer                          *   value                                * pointer to byte    
     D noofbytes                     10U 0 value                                * 32 bits            
                                                                                                     
     D open            PR            10I 0 extproc('open')                                           
     D ifspath                         *   value options(*string)               *ifs path            
     D oflag                         10I 0 value                                *string of 32 bits   
     D mode                          10U 0 value options(*nopass)               * 9 bits             
     D codepage                      10U 0 value options(*nopass)               *                    
                                                                                                     
      * <-----oflag---->                                                                             
     D O_readonly      C                   1                                                         
     D O_writeonly     C                   2                                                         
     D O_readwrite     C                   4                     
     D O_createfileifnotexist...                                 
     D                 C                   8                     
     D O_exclusivecreate...                                      
     D                 C                   16                    
     D O_truncateto0bytes...                                     
     D                 C                   64                    
     D O_appendtofile  C                   256                   
     D O_converttextbycodepage...                                
     D                 C                   8388608               
     D O_openintextmode...                                       
     D                 C                   16777216              
      *                                                          
      * <-----mode---->                                          
      * owner,group,other (RWX)                                  
      *                                         owner authority  
     D M_readowner     C                   256                   
     D M_writeowner    C                   128                   
     D M_executeowner  C                   64                    
      *                                         group authority  
     D M_readgroup     C                   32                
     D M_writegroup    C                   16                
     D M_executegroup  C                   8                 
      *                                         other people 
     D M_readother     C                   4                 
     D M_writeother    C                   2                 
     D M_executeother  C                   1                 
      *                                                      
     Difspath          s            512a                     
     Doflag            s             10I 0                   
     Dmode             s             10U 0                   
     Dcodepage         s             10U 0                   
     Dfiledescriptor   s             10i 0                   
     Difsdata          s             11a   inz               
     Dreturn_write     s             10i 0 inz               
     Dreturn_close     s             10i 0 inz               
      /free                                                  
       ifspath = '/home/easyclass/append';                   
       oflag = O_writeonly +                                 
               O_createfileifnotexist +                      
               O_appendtofile ;                                                
       mode =                                                                  
              M_readowner +                                                    
              M_writeowner +                                                   
              M_executeowner;                                                  
                                                                               
       filedescriptor = open(%trim(ifspath):                                   
                             oflag:                                            
                             mode);                                            
       if filedescriptor < 0;                                                  
         return;                                                               
       endif;                                                                  
                                                                               
       ifsdata = 'DATA APPEND';                                                
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));     
       if return_write < %size(ifsdata);                                       
         return;                                                               
       endif;                                                                  
       ifsdata = 'CONTINUED';                                                  
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));     
       if return_write < %size(ifsdata);       
         return;                               
       endif;                                  
                                               
       return_close = close(filedescriptor);   
       if return_close = -1;                   
         return;                               
       endif;                                  
                                               
       *inlr = *on;                            
       return;                                 
      /end-free                                                                                                        
RPG Code in Fully Free format to Append data to a IFS stream file in RPGLE.
**FREE                                       
CTL-OPT DFTACTGRP(*NO);                      
                                             
DCL-PR close int(10) EXTPROC('close');       
  fileds int(10) VALUE;                      
END-PR;                                      
                                             
DCL-PR write int(10) EXTPROC('write');       
  fileds  int(10) VALUE;                     
  buffer  pointer VALUE;                     
  noofbytes uns(10) VALUE;                   
END-PR;                                      
                                             
DCL-PR open int(10) EXTPROC('open');         
  ifspath pointer VALUE options(*string);    
  oflag   int(10) VALUE;                     
  mode uns(10) VALUE options(*nopass);       
  codepage uns(10) VALUE options(*nopass);   
END-PR;                                      
// * <-----oflag---->                                             
DCL-C O_readonly 1;                                               
DCL-C O_writeonly 2;                                              
DCL-C O_readwrite 4;                                              
DCL-C O_createfileifnotexist 8;                                   
DCL-C O_exclusivecreate 16;                                       
DCL-C O_truncateto0bytes 64;                                      
DCL-C O_appendtofile  256;                                        
DCL-C O_converttextbycodepage 8388608;                            
DCL-C O_openintextmode 16777216;                                  
//    * <-----mode---->                                          
//    * owner,group,other (RWX)                                   
//    *                                         owner authority   
DCL-C M_readowner 256;                                            
DCL-C M_writeowner 128;                                           
DCL-C M_executeowner 64;                                          
//    *                                         group authority   
DCL-C M_readgroup 32;                                             
DCL-C M_writegroup 16;                                            
DCL-C M_executegroup 8;                                           
//    *                                         other people     
DCL-C M_readother 4;                                             
DCL-C M_writeother 2;                                            
DCL-C M_executeother 1;                                          
                                                                 
DCL-S ifspath CHAR(512);                                         
DCL-S oflag int(10);                                             
DCL-S mode  uns(10);                                             
DCL-S codepage uns(10);                                          
DCL-S filedescriptor int(10);                                    
DCL-S ifsdata char(500) inz;                                     
DCL-S return_write int(10) inz;                                  
DCL-S return_close int(10) inz;                                  
                                                                 
       ifspath = '/home/easyclass/append';                       
       oflag = O_writeonly +                                     
               O_createfileifnotexist +                          
               O_appendtofile ;                                  
       mode =                                                    
              M_readowner +                                      
              M_writeowner +                                              
              M_executeowner;                                             
                                                                          
       filedescriptor = open(%trim(ifspath):                              
                             oflag:                                       
                             mode);                                       
       if filedescriptor < 0;                                             
         return;                                                          
       endif;                                                             
                                                                          
       ifsdata = 'DATA APPEND';                                           
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));
       if return_write < %size(ifsdata);                                  
         return;                                                          
       endif;                                                             
       ifsdata = 'CONTINUED';                                             
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));
       if return_write < %size(ifsdata);                                  
         return;                                                          
       endif;                                                             
                                                
       return_close = close(filedescriptor);    
       if return_close = -1;                    
         return;                                
       endif;                                   
                                                
       *inlr = *on;                             
       return;                                                              

Explanation of the above code

       ifspath = '/home/easyclass/append';                       
       oflag = O_writeonly +                                     
               O_createfileifnotexist +                          
               O_appendtofile ;                                  
       mode =                                                    
              M_readowner +                                      
              M_writeowner +                                              
              M_executeowner;                                             
                                                                          
       filedescriptor = open(%trim(ifspath):                              
                             oflag:                                       
                             mode);                                       
       if filedescriptor < 0;                                             
         return;                                                          
       endif;                           

Here, at first we will create the ifs stream file if not exist and open it in write only mode and will append the data to the file during write instead of replacing the existing content in the ifs stream file. Therefore we pass O_writeonly + O_createfileifnotexist + O_appendtofile ; flags to the openflag second parameter of the open() api. Other parameters being passed are ifspath for the file named "append" on the user home directory and mode parameter i.e. authority for the file to the owner as read, write and execute. If the filedescriptor returned by the open() api is less than zero then api either fails tocreate or open the stream file with specified flags and we return from the program here since file is not opened therefore, no sense of proceeding forward.

       ifsdata = 'DATA APPEND';                                           
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));
       if return_write < %size(ifsdata);                                  
         return;                                                          
       endif;                                                             
       ifsdata = 'CONTINUED';                                             
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));
       if return_write < %size(ifsdata);                                  
         return;                                                          
       endif;    

Here, at next step after opening the file successfully, we will append the data to the ifs stream file using the write() api by passing file descriptor being returned by the open() api and address and size of the ifsdata variable to be written to the disk. If return value by write() api is less than the size of the ifsdata variable then that means all the data is not appended to the ifs stream file therefore, return from the program at this point otherwise proceed further and append the data again to the stream file.

       return_close = close(filedescriptor);    
       if return_close = -1;                    
         return;                                
       endif;                                   
                                                
       *inlr = *on;                             
       return;       

Finally close the file by using close() api and passing file descriptor returned by the open() api. If return value of close() api is -1 then return abnormally otherwise end program normally by first setting last record indicator to ON and return from the program.

Output of the above program

The file already exist and having already some data

 Browse : /home/EASYCLASS/append                                                        
 Record :       1   of       1 by  18                      Column :    1     59 by 131  
 Control :                                                                              
                                                                                        
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+...
 ************Beginning of data**************                                            
DATA APPENDCONTINUED                                                                    
 ************End of Data********************                                            

Let's call the program again (use option 14 to create the program) to append the data to the ifs stream file.

 Browse : /home/EASYCLASS/append                                                        
 Record :       1   of       1 by  18                      Column :    1     59 by 131  
 Control :                                                                              
                                                                                        
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+...
 ************Beginning of data**************                                            
DATA APPENDCONTINUED  DATA APPENDCONTINUED                                              
 ************End of Data********************                                            

Post a Comment

© AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain