Create and write Text files in on IFS in RPGLE

Create and write Text files in on IFS in RPGLE.
Create and write Text files in IFS, RPGLE, write data into the IFS file, Reads the file, Closes the file, open(), write(), read(), close(), read() procedure in rpgle,  open(), open() api, file descriptor, ifs stream file, reading data from ifs stream file using rpg as400 in ibmi, c apis, as400, ibmi, as400 and sql tricks, as400 tutorial, ibmi tutorial, working with if, integrated file system,reading streams with the read() API,The read() API is used to read bytes from a stream file,UNIX-type APIs,C language prototype of read() ,RPG version of the prototype read() api,extproc,Working with the IFS in RPG IV, prototyping of read() api,The path parameter,The oflag parameter,The mode parameter, The codepage parameter,The return value of the read() API,Code snippet showing the use of the read() API,Introduction to the IFS, Code snippet showing the use of the write()
Create and write Text files in IFS

A Text file is a simple flat file that is composed of lines of the text. Text files must have end-of-line delimeters or new line character. Microsoft Windows text files use a common text file format with each line of text separated by a two character combination i.e. CR (Carriage return) and LF (Line Feed). The extension for the text file is .txt. Therefore, must write text followed by CR and LF characters.

Steps involved in create and write Text files in IFS

  1. First, create and open the ifs stream file in write only mode using C open() api.
  2. Second, write the data to the ifs stream file using C write()api followed by CR and LF characters.
  3. Third, close the ifs stream file using C close() api.

RPGLE program for create and write Text files in IFS

RPG Code in Fixed format to Create and write Text files on IFS.
     HDFTACTGRP(*NO)                                                                                  
                                                                                                      
     D errorifs        PR              *   ExtProc('__errno')                                         
                                                                                                      
     D strerror        PR              *   ExtProc('strerror')                                        
     D error_num                     10I 0 value                                                      
                                                                                                      
     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            400a   inz                         
     Dreturn_write     s             10i 0 inz                         
     Dreturn_close     s             10i 0 inz                                
                                                                              
     Derror_ptr        S               *                                      
     Derror_num        S             10I 0 based(error_ptr)                   
                                                                              
     Derrormsg_ptr     S               *                                      
     Derror_msg        S            500a   based(errormsg_ptr)                
                                                                              
     DCRLF             C                   CONST(x'0D25')                     
                                                                              
     C                   EVAL      ifspath = '/home/easyclass/txtfile1.txt'   
     C                   EVAL      oflag = O_writeonly +                      
     C                                     O_createfileifnotexist             
     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                   EVAL      error_ptr = errorIFS()                    
     C                   EVAL      errormsg_ptr = strerror(error_num)        
     C                   RETURN                                              
     C                   ENDIF                                               
     C                                                                       
     C                   EVAL      ifsdata = 'IFS TEXT FILE1' + CRLF +       
     C                             'IFS TEXT FILE2' + CRLF +                 
     C                             CRLF  + 'END' + CRLF                      
     C                   EVAL      return_write = write(filedescriptor:      
     C                             %addr(ifsdata):%size(ifsdata))            
     C                   IF        return_write < %size(ifsdata)             
     C                   EVAL      error_ptr = errorIFS()                    
     C                   EVAL      errormsg_ptr = strerror(error_num)        
     C                   RETURN                                              
     C                   ENDIF                                               
     C                                                                       
     C                   EVAL      return_close = close(filedescriptor)      
     C                   IF        return_close = -1                         
     C                   EVAL      error_ptr = errorIFS()                    
     C                   EVAL      errormsg_ptr = strerror(error_num)        
     C                   RETURN                        
     C                   ENDIF                         
                                                       
     C                   EVAL      *INLR = *ON         
     C                   RETURN                                                                                                      
RPG Code in /Free and /End-Free format to Create and write Text files on IFS.
     HDFTACTGRP(*NO)                                                                                  
                                                                                                      
     D errorifs        PR              *   ExtProc('__errno')                                         
                                                                                                      
     D strerror        PR              *   ExtProc('strerror')                                        
     D error_num                     10I 0 value                                                      
                                                                                                      
     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            400a   inz                         
     Dreturn_write     s             10i 0 inz                         
     Dreturn_close     s             10i 0 inz                                
                                                                              
     Derror_ptr        S               *                                      
     Derror_num        S             10I 0 based(error_ptr)                   
                                                                              
     Derrormsg_ptr     S               *                                      
     Derror_msg        S            500a   based(errormsg_ptr)                
                                                                              
     DCRLF             C                   CONST(x'0D25')              
      /free                                          
       ifspath = '/home/easyclass/txtfile1.txt';     
       oflag = O_writeonly +                         
               O_createfileifnotexist;               
       mode =                                        
              M_readowner +                          
              M_writeowner +                         
              M_executeowner;                                               
                                                                            
       filedescriptor = open(%trim(ifspath):                                
                             oflag:                                         
                             mode);                                         
       if filedescriptor < 0;                                               
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
         return;                                                            
       endif;                                                               
                                                                            
       ifsdata = 'IFS TEXT FILE1' + CRLF +                                  
                 'IFS TEXT FILE2' + CRLF +                                  
                                    CRLF  + 'END' + CRLF;                   
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));  
       if return_write < %size(ifsdata);                                    
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
         return;                                                            
       endif;                                                               
                                                    
       return_close = close(filedescriptor);        
       if return_close = -1;                        
         error_ptr = errorIFS();                    
         errormsg_ptr = strerror(error_num);        
         return;                                    
       endif;                                       
                                                    
       *inlr = *on;                                 
       return;                                      
      /end-free                                                                                                                                                            
RPG Code in Fully Free format to Create and write Text files on IFS.
**FREE                                          
CTL-OPT DFTACTGRP(*NO);                         
                                                
DCL-PR errorifs pointer EXTPROC('__errno');     
END-PR;                                         
                                                
DCL-PR strerror pointer EXTPROC('strerror');    
  error_num int(10) VALUE;                      
END-PR;                                         
                                                
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;                    
DCL-S error_ptr pointer;                           
DCL-S error_num int(10) based(error_ptr);          
DCL-S errormsg_ptr pointer;                        
DCL-S error_msg char(500) based(errormsg_ptr);     
                                                   
DCL-C CRLF CONST(x'0D25');                         
                                                   
       ifspath = '/home/easyclass/txtfile1.txt';   
       oflag = O_writeonly +                       
               O_createfileifnotexist;             
       mode =                                      
              M_readowner +                        
              M_writeowner +                       
              M_executeowner;                      
                                                   
       filedescriptor = open(%trim(ifspath):       
                             oflag:                
                             mode);                
       if filedescriptor < 0;                                                
         error_ptr = errorIFS();                                             
         errormsg_ptr = strerror(error_num);                                 
         return;                                                             
       endif;                                                                
                                                                             
       ifsdata = 'IFS TEXT FILE1' + CRLF +                                   
                 'IFS TEXT FILE2' + CRLF +                                   
                                    CRLF  + 'END' + CRLF;                    
       return_write = write(filedescriptor:%addr(ifsdata):%size(ifsdata));   
       if return_write < %size(ifsdata);                                     
         error_ptr = errorIFS();                                             
         errormsg_ptr = strerror(error_num);                                 
         return;                                                             
       endif;                                                                
                                                                             
       return_close = close(filedescriptor);                                 
       if return_close = -1;                                                 
         error_ptr = errorIFS();                                             
         errormsg_ptr = strerror(error_num);                                 
         return;           
       endif;              
                           
       *inlr = *on;        
       return;                                               

Here, if we look at the below data, 'IFS TEXT FILE1' will be written on the 1st line then CRLF means start 2nd line and then 'IFS TEXT FILE2' will be on 2nd line and then CRLF means start third line and that will be blank since we encounter next CRLF and that starts 4th line and 'END' will be on the 4th line and then atlast CRLF and that will start 5th line and that will be blank as the CRLF is basically denotes start of new line.

       ifsdata = 'IFS TEXT FILE1' + CRLF +                                   
                 'IFS TEXT FILE2' + CRLF +                                   
                                    CRLF  + 'END' + CRLF;             
  • Output text file on IFS
  •  Browse : /home/EASYCLASS/txtfile1.txt                                                                                             
     Record :       1   of       5 by  18                      Column :    1    361 by 131                                             
     Control :                                                                                                                         
                                                                                                                                       
    ....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+....9....+....0....+....1....+....2....+....3.
     ************Beginning of data**************                                                                                       
    IFS TEXT FILE1                                                                                                                     
    IFS TEXT FILE2                                                                                                                     
                                                                                                                                       
    END                                                                                                                                
                                                                                                                                       
     ************End of Data********************                                                                                       

    Post a Comment

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