Reading Text Files in IFS in RPGLE

Reading Text Files in IFS in RPGLE.
Reading Text Files in IFS, RPGLE program for Read text file on IFS, 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,
Reading Text Files in IFS

We already created text file named txtfile1.txt on ifs and written Text data.

  • /home/easyclass/txtfile1.txt
  •  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                                                                                                                                
    

    Now, In this article we will learn how to read the text files on the ifs in RPGLE program. Let's analyze the below program that performs read operation on the text file present on ifs.

    RPGLE program for Reading text file on IFS

    RPG Code in Fixed format for reading text file present on ifs.
         HDFTACTGRP(*NO)                                                                                   
                                                                                                           
         D errorifs        PR              *   ExtProc('__errno')                                          
                                                                                                           
         D strerror        PR              *   ExtProc('strerror')                                         
         D error_num                     10I 0 value                                                       
                                                                                                           
         D read            PR            10i 0 extproc('read')                      * 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 close           PR            10i 0 extproc('close')                     *                      
         D fileds                        10i 0 value                                *file descriptor       
                                                                                                           
         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             10a   inz                             
         Dreturn_read      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)                
                                                                                  
         DCR               C                   CONST(x'0D')                       
         DLF               C                   CONST(x'25')                       
         Dtextline         S            500a   inz                                
         Dposition         S             10I 0 inz                                
         Dlength           S             10I 0 inz                                
                                                                                  
         C                   EVAL      ifspath = '/home/easyclass/txtfile1.txt'   
         C                   EVAL      oflag = O_readonly                         
                                                                                  
         C                   EVAL      filedescriptor = open(%trim(ifspath):      
         C                             oflag)                                     
                                                                                           
         C                   IF        filedescriptor < 0                                  
         C                   EVAL      error_ptr = errorIFS()                              
         C                   EVAL      errormsg_ptr = strerror(error_num)                  
         C                   RETURN                                                        
         C                   ENDIF                                                         
                                                                                           
         C                   EVAL      return_read  = read(filedescriptor:                 
         C                                            %addr(ifsdata):                      
         C                                            %size(ifsdata))                      
         C                   EVAL      position = 1                                        
                                                                                           
         C                   DOW       position <= return_read                             
                                                                                           
         C                   IF        %SUBST(ifsdata:position:1) = CR                     
         C                   CLEAR                   textline                              
         C                   RESET                   length                                
         C                   ELSE                                                          
         C                   IF        %SUBST(ifsdata:position:1) <> LF                    
         C                   EVAL      length = length + 1                                 
         C                   EVAL      %SUBST(textline:length:1) =                    
         C                                    %SUBST(ifsdata:position:1)              
         C                   ENDIF                                                    
         C                   ENDIF                                                    
                                                                                      
         C                   IF        position = return_read                         
         C                   RESET                   position                         
         C                   EVAL      return_read  = read(filedescriptor:            
         C                                          %addr(ifsdata):%size(ifsdata))    
         C                   ENDIF                                                    
                                                                                      
         C                   EVAL      position = position + 1                        
                                                                                      
         C                   ENDDO                                                    
                                                                                      
         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 for reading text file present on ifs.
         HDFTACTGRP(*NO)                                                                                   
                                                                                                           
         D errorifs        PR              *   ExtProc('__errno')                                          
                                                                                                           
         D strerror        PR              *   ExtProc('strerror')                                         
         D error_num                     10I 0 value                                                       
                                                                                                           
         D read            PR            10i 0 extproc('read')                      * 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 close           PR            10i 0 extproc('close')                     *                      
         D fileds                        10i 0 value                                *file descriptor       
                                                                                                           
         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             10a   inz                             
         Dreturn_read      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)                
                                                                                  
         DCR               C                   CONST(x'0D')                       
         DLF               C                   CONST(x'25')                       
         Dtextline         S            500a   inz                                
         Dposition         S             10I 0 inz                                
         Dlength           S             10I 0 inz  
          /free                                                                
           ifspath = '/home/easyclass/txtfile1.txt';                           
           oflag = O_readonly;                                                 
                                                                               
           filedescriptor = open(%trim(ifspath):                               
                                 oflag);                                       
           if filedescriptor < 0;                                              
             error_ptr = errorIFS();                                           
             errormsg_ptr = strerror(error_num);                               
             return;                                                           
           endif;                                                              
                                                                               
           return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));  
                                                                               
           position = 1;                                                       
           dow position <= return_read;                                               
                                                                                      
             if %SUBST(ifsdata:position:1) = CR;                                      
              clear textline;                                                         
              reset length;                                                           
             else;                                                                    
               if %SUBST(ifsdata:position:1) <> LF;                                   
                 length = length + 1;                                                 
                 %SUBST(textline:length:1) = %SUBST(ifsdata:position:1);              
               endif;                                                                 
             endif;                                                                   
                                                                                      
             if position = return_read;                                               
               reset position;                                                        
               return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));     
             endif;                                                                   
             position = position + 1;                                                 
           enddo;                                                                     
                                                                                      
           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 for reading text file present 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 read int(10) EXTPROC('read');           
      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;                                      
                                                 
    DCL-PR close int(10) EXTPROC('close');       
      fileds int(10) VALUE;                      
    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_read 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-S position int(10) inz;                              
    DCL-S length int(10) inz;                                
    DCL-S textline char(500) inz;                            
                                                             
    DCL-C CR CONST(x'0D');                                   
    DCL-C LF CONST(x'25');                                   
                                                             
           ifspath = '/home/easyclass/txtfile1.txt';         
           oflag = O_readonly;                               
                                                             
           filedescriptor = open(%trim(ifspath):             
                                 oflag);                                       
           if filedescriptor < 0;                                              
             error_ptr = errorIFS();                                           
             errormsg_ptr = strerror(error_num);                               
             return;                                                           
           endif;                                                              
                                                                               
           return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));  
                                                                               
           position = 1;                                                       
           dow position <= return_read;                                        
                                                                               
             if %SUBST(ifsdata:position:1) = CR;                               
              clear textline;                                                  
              reset length;                                                    
             else;                                                             
               if %SUBST(ifsdata:position:1) <> LF;                            
                 length = length + 1;                                          
                 %SUBST(textline:length:1) = %SUBST(ifsdata:position:1);       
               endif;                                                          
             endif;                                                                
                                                                                   
             if position = return_read;                                            
               reset position;                                                     
               return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));  
             endif;                                                                
             position = position + 1;                                              
           enddo;                                                                  
                                                                                   
           return_close = close(filedescriptor);                                   
           if return_close = -1;                                                   
             error_ptr = errorIFS();                                               
             errormsg_ptr = strerror(error_num);                                   
             return;                                                               
           endif;                                                                  
                                                                                   
           *inlr = *on;                                                            
           return;                                                                                          
    

    Compile-Run-Output of the above program

  • Compile the above RPGLE program by either using command CRTSQLRPGI or option 14 on source member of type SQLRPGLE.
  • Call the program using call command CALL <program name> from the IBM i command line.
  • For output, Please debug the program and check the data being loaded in variable ifsdata.
  • Brief explanation of the code

  • Assign the text file name in ifs path with its full ifs path and open the text file named txtfile1.txt in readonly mode using open() api.
  • Perform error handling by calling error handling apis when filedescriptor is less than zero and return from the program at this point otherwise proceed further in the program to read ifs text file.
  • Read the ifs file using READ() api by passing filedescriptor returned by open() api as first and address of varible named ifsdata as second and size of variable named ifsdata as third parameter to this READ() api.
  • Now, we have performed first read this is not done yet since we need to read the whole text file on the ifs so we will read the file in loop.For reading in loop, we first set a variable named position as 1 i.e. from start and run the loop from position 1 upto the return_read value i.e. no. of bytes returned by read() api at first read operation. Inside the loop, we check in IF condition whether the currently looped byte being read in variable ifsdata contains CR charcter (Carriage Return) then take a variable named textline where we extract data of each line before CR character and clear this variable. we do also reset length variable and in else part we check where currently looped byte in variable ifsdata does not contain LF character (Line Feed) then only add 1 to the length variable means this is the first character aned evaluate ifsdata 1st character to the textline 1st position and so on until the LF arrives and as we already clearing and resetting textline and length variable for every next CR encountered in the ifsdata variable by incrementing position variable byy 1. Now, within the loop we check if positon = return_read means we read each single byte returned in ifsdata by the first read operation in loop and identified each line data separately by using CRLF characters presence check which signifies the start of next line in text files then we reset the position and again call the read() api to read the next specified bytes of data from ifs text file to the variable ifsdata and then loop ends with ENDDO opcode.
  • Finally, call the close() api by passing paramater as filedescriptor to the close() api. If the return value from close() api is -1 then call the error handling apis to get the error number and error message and return from the program otherwise end normally by setting last record indicator to ON and return.
  • Post a Comment

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