Reading IFS stream file randomly using C API LSEEK() in RPGLE

Reading IFS stream file randomly using C API LSEEK() in RPGLE.
Reading IFS stream file randomly using C API LSEEK in RPGLE, lseek() Prototype for the lseek() API in C, RPGLE program forPosition to offset in a IFS stream file,  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,
Reading IFS stream file randomly using C API LSEEK in RPGLE

As we all know that in RPGLE we can read a file without any key fields randomly by setting the positions by the RRN (record relative number) using SETLL and SETGT opcodes. Here, we use the term "randomly" that means we can read any record at any time without having to read them sequentially. The alternative for reading the ifs stream file randomly using C api is the lseek() api. As we all know that ifs stream files are not organized as records, therefore we use offset i.e. number of bytes to move with lseek() api instead of RRN.

Position to a given offset in a IFS stream file using C lseek() API in RPGLE

lseek() is used to change the location of the read/write pointer of a file descriptor and specify new file offsets past the start/current end of the file.

Prototype for the lseek() API in C Language

off_t lseek(int file_descriptor, off_t offset, int whence);
  1. off_t: (return value) The return value would be the new offset from the start of the file if lseek() is successful, otherwise it will be -1.
  2. lseek() function changes the current file offset to a new position in the file.
  3. int fildes :(Input) The file descriptor of the pointer that is opened using open() api.
  4. off_t offset :(Input) The offset is the number of bytes to move forward or backward in the ifs stream file from the point that is specified in the next parameter named whence.For moving forward we set the positive number and for moving backward we set the negative number and atlast for moving to the point specified in the next paramter named whence we need to set the offset as zero.
  5. int whence :(Input) This parameter tells where we want to move or start adding offset from the position. We can use three named constant for setting the value in this parameter.
    • SEEK_SET: It means that the the offset would be from the start of the file. We set the offset as 0 which means start from the first byre if the file.
    • SEEK_CUR: It means that the offset would be from the current position in the file.Let's say if we want to read the last byte again we would use SEEK_CUR with an offset of -1.
    • SEEK_END:It means that the offset would be from the end of the file.
         D SEEK_SET        C                   CONST(0)   
         D SEEK_CUR        C                   CONST(1)   
         D SEEK_END        C                   CONST(2)   

    Basically, When we use SEEK_SET which means we want the offset to be set from the start of the file, therefore we won't set any bits to ON. In case of SEEK_CUR, we set the 1st bit from the right to set ON so that the offset would be set from the current positions in the file. In case of SEEK_END, we set the 2nd bit from the right to set ON so that the offset would be set from the end of the file.

Prototype for the lseek() API in RPGLE

     D lseek           PR            10I 0 ExtProc('lseek')
     D filedescriptor                10I 0 value           
     D offset                        10I 0 value           
     D whence                        10I 0 value           

RPGLE program for Position to offset in a IFS stream file

RPG Code in Fixed format for Reading IFS stream file randomly using C API LSEEK() 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)               *                      
                                                                                                       
     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 errorifs        PR              *   ExtProc('__errno')                                
                                                                                             
     D strerror        PR              *   ExtProc('strerror')                               
     D error_num                     10I 0 value                                             

     D lseek           PR            10I 0 ExtProc('lseek')                                  
     D filedescriptor                10I 0 value                                             
     D offset                        10I 0 value                                             
     D whence                        10I 0 value                                  
                                                                                                                                 
      * <-----lseek constant---->                                                                                      
     D SEEK_SET        C                   CONST(0)                                          
     D SEEK_CUR        C                   CONST(1)                                          
     D SEEK_END        C                   CONST(2)         
                                                            
      * <-----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             41a   inz                      
     Dreturn_write     s             10i 0 inz                      
     Dreturn_read      s             10i 0 inz                                    
     Dreturn_close     s             10i 0 inz                                    
     Dreturn_lseek     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)           
                                                                                  
     C                   EVAL      ifspath = '/home/easyclass/lseek'              
     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                   EVAL      ifsdata = 'LSEEK C API DEMO IN RPG AS400 P' +  
     C                                       'ROGRAMMING'                            
     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                   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      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                   CLEAR                   IFSDATA                        
     C                   EVAL      return_read  = read(filedescriptor:          
     C                             %addr(ifsdata):%size(ifsdata))               
     C                   IF        return_read < 1                              
     C                   EVAL      error_ptr = errorIFS()                       
     C                   EVAL      errormsg_ptr = strerror(error_num)           
     C                   ENDIF                                                  
                                                                                
     C                   EVAL      return_lseek = lseek(filedescriptor:10:      
     C                                            SEEK_SET)                     
     C                   IF        return_lseek < 0                             
     C                   EVAL      error_ptr = errorIFS()                       
     C                   EVAL      errormsg_ptr = strerror(error_num)           
     C                   ENDIF                                                  
                                                                                
     C                   CLEAR                   IFSDATA                   
     C                   EVAL      return_read  = read(filedescriptor:     
     C                             %addr(ifsdata):10)                      
     C                   IF        return_read < 1                         
     C                   EVAL      error_ptr = errorIFS()                  
     C                   EVAL      errormsg_ptr = strerror(error_num)      
     C                   ENDIF                                             
                                                                           
     C                   EVAL      return_lseek = lseek(filedescriptor:10: 
     C                                            SEEK_CUR)                
     C                   IF        return_lseek < 0                        
     C                   EVAL      error_ptr = errorIFS()                  
     C                   EVAL      errormsg_ptr = strerror(error_num)      
     C                   ENDIF                                             
                                                                           
     C                   CLEAR                   IFSDATA                   
     C                   EVAL      return_read  = read(filedescriptor:     
     C                             %addr(ifsdata):%size(ifsdata))          
     C                   IF        return_read < 1                         
     C                   EVAL      error_ptr = errorIFS()                  
     C                   EVAL      errormsg_ptr = strerror(error_num)       
     C                   ENDIF                                              
                                                                            
     C                   EVAL      return_lseek = lseek(filedescriptor:-10: 
     C                                            SEEK_END)                 
     C                   IF        return_lseek < 0                         
     C                   EVAL      error_ptr = errorIFS()                   
     C                   EVAL      errormsg_ptr = strerror(error_num)       
     C                   ENDIF                                              
                                                                            
     C                   CLEAR                   IFSDATA                    
     C                   EVAL      return_read  = read(filedescriptor:      
     C                             %addr(ifsdata):%size(ifsdata))           
     C                   IF        return_read < 1                          
     C                   EVAL      error_ptr = errorIFS()                   
     C                   EVAL      errormsg_ptr = strerror(error_num)       
     C                   ENDIF                                              
                                                                            
     C                   EVAL      *INLR = *ON                              
     C                   RETURN                                                                                                                   
RPG Code in /Free and /End-Free format for Reading IFS stream file randomly using C API LSEEK() 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)               *                      
                                                                                                       
     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 errorifs        PR              *   ExtProc('__errno')                                
                                                                                             
     D strerror        PR              *   ExtProc('strerror')                               
     D error_num                     10I 0 value                                             
                                                                                             
     Derror_ptr        S               *                                                     
     Derror_num        S             10I 0 based(error_ptr)                                  
                                                                                             
     Derrormsg_ptr     S               *                                                     
     Derror_msg        S            500a   based(errormsg_ptr)                               
                                                                                             
     D lseek           PR            10I 0 ExtProc('lseek')                                  
     D filedescriptor                10I 0 value                                             
     D offset                        10I 0 value                                             
     D whence                        10I 0 value                                             
                                                                                             
     D SEEK_SET        C                   CONST(0)                                          
     D SEEK_CUR        C                   CONST(1)                                          
     D SEEK_END        C                   CONST(2)         
                                                            
      * <-----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             41a   inz                      
     Dreturn_write     s             10i 0 inz                      
     Dreturn_read      s             10i 0 inz                                    
     Dreturn_close     s             10i 0 inz                                    
     Dreturn_lseek     s             10i 0 inz              
      /free                                       
       ifspath = '/home/easyclass/lseek';         
       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 = 'LSEEK C API DEMO IN RPG AS400 PROGRAMMING';                      
       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;                                                                      
                                                                                                                                                                  
       oflag = O_readonly ;                                                        
       filedescriptor = open(%trim(ifspath):                                       
                             oflag);                                               
                                                                            
       if filedescriptor < 0;                                               
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
         return;                                                            
       endif;                                                               
                                                                            
       clear ifsdata;                                                       
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));   
       if return_read < 1;                                                  
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               
                                                                            
       return_lseek = lseek(filedescriptor:10:SEEK_SET);                    
       if return_lseek < 0;                                                 
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               
                                                        
       clear ifsdata;                                                       
       return_read  = read(filedescriptor:%addr(ifsdata):10);               
       if return_read < 1;                                                  
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               
                                                                            
       return_lseek = lseek(filedescriptor:10:SEEK_CUR);                    
       if return_lseek < 0;                                                 
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               
                                                                            
       clear ifsdata;                                                       
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata));   
       if return_read < 1;                                                  
         error_ptr = errorIFS();                                            
         errormsg_ptr = strerror(error_num);                                
       endif;                                                               

       return_lseek = lseek(filedescriptor:-10:SEEK_END);                 
       if return_lseek < 0;                                               
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       clear ifsdata;                                                     
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); 
       if return_read < 1;                                                
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       *inlr = *on;                                                       
       return;                                                            
      /end-free                                                                                                                                                                         
RPG Code in Fully Free format for Reading IFS stream file randomly using C API LSEEK() 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;                                      
                                             
DCL-PR read int(10) EXTPROC('read');            
  fileds  int(10) VALUE;                        
  buffer  pointer VALUE;                        
  noofbytes uns(10) VALUE;                      
END-PR;                                         
                                                
DCL-PR lseek int(10) EXTPROC('lseek');          
  filedescriptor int(10) VALUE;                 
  offset int(10) VALUE;                         
  whence int(10) VALUE;                         
END-PR;                                         
                                                
DCL-PR errorifs pointer EXTPROC('__errno');     
END-PR;                                         
                                                
DCL-PR strerror pointer EXTPROC('strerror');    
  error_num int(10) VALUE;                      
END-PR;                                         
                                                
// * <-----lseek constant---->                  
DCL-C SEEK_SET 0;                                                     
DCL-C SEEK_CUR 1;                                                     
DCL-C SEEK_END 2;                                                     
                                                                      
// * <-----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_read int(10) inz;                                      
DCL-S return_close int(10) inz;                                     
DCL-S return_lseek 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);  
                                                
       ifspath = '/home/easyclass/lseek';       
       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 = 'LSEEK C API DEMO IN RPG AS400 PROGRAMMING';              
       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;                                                              
                                                                                                                                              
       oflag = O_readonly ;      
       filedescriptor = open(%trim(ifspath):                              
                             oflag);                                      
                                                                          
       if filedescriptor < 0;                                             
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
         return;                                                          
       endif;                                                             
                                                                          
       clear ifsdata;                                                     
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); 
       if return_read < 1;                                                
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_lseek = lseek(filedescriptor:10:SEEK_SET);                  
       if return_lseek < 0;                                               
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       clear ifsdata;                                                     
       return_read  = read(filedescriptor:%addr(ifsdata):10);             
       if return_read < 1;                                                
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_lseek = lseek(filedescriptor:10:SEEK_CUR);                  
       if return_lseek < 0;                                               
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       clear ifsdata;                                                     
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); 
       if return_read < 1;                                                
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       return_lseek = lseek(filedescriptor:-10:SEEK_END);                 
       if return_lseek < 0;                                               
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       clear ifsdata;                                                     
       return_read  = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); 
       if return_read < 1;                                                
         error_ptr = errorIFS();                                          
         errormsg_ptr = strerror(error_num);                              
       endif;                                                             
                                                                          
       *inlr = *on;                                                       
       return;                                                                                                      

Explanation of the above code

  1. We set the ifspath of the open() api as /home/easyclass/lseek where file name is lseek in the user home directory.
  2. Then we set the oflag parameter of the open() api as O_writeonly + O_createfileifnotexist means we create the file named lseek first on the ifs path if not present and then opened it in write only mode for writing data to the ifs stream file.
  3. Then we set the mode parameter of the open() api as M_readowner + M_writeowner + M_executeowner to provide read, write and execute authority to the owner on the file lseek being created in the user home directory specified in the ifspath parameter of the open() api.
  4. Then we call the open() api by passing ifspath, oflag and mode parameter to open create the file and open the file in the write only mode. If the file being created successfully and opened in write only mode then the return value by the open() api i.e. file descriptor would be greater or equals zero otherwise the file descriptor returned as -1 means error.
  5. Check if file descriptor returned is less than zero then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number and return from the program.
  6. If the file is created successfully if not present at the ifs path and being opened in write only mode then proceed with writing the data to the ifs stream file. Set the ifsdata as ifsdata = 'LSEEK C API DEMO IN RPG AS400 PROGRAMMING';
  7. Call the write() api with parameters as file descriptor returned by the open() api, address of the ifsdata variable and size of the ifsdata variable. This api will return the no. of bytes actually being written to the ifs stream file.
  8. If the no. of bytes being return is less than the size of the ifsdata varible being passed to the write() api as 3rd parameter then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number and return from the program.
  9. Then call the close() api by passing file descriptor returned by pen() api as a parameter. This api will return 0 if file gets closed and -1 if failed to close the stream file.
  10. If the return value from close() api is -1 then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number and return from the program.
  11. Again set the Oflag as O_readonly i.e. readonly and call the open() api by passing ifspath and oflag to open the file in read only mode this time. File descriptor being return by the api.
  12. If the file descriptor being returned is less than zero means failed to open the file in read only mode then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number and return from the program.
  13. Clear the ifsdata variable and call the read() api and pass parameters as file descriptor returned from open() api and address of the ifsdata varible and size of the ifsdata variable. This api will return the no. of bytes actually being read. Here we are reading the file sequentially. if the bytes being read is less than 1 then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number. However, there would be chance that file is empty or we are already at the end of file. Here, we get to the read all the data "LSEEK C API DEMO IN RPG AS400 PROGRAMMING" that we do wrote initially.
  14. Now for reading file randomly, call the lseek() api this time with parameters as file descriptor, offset as 10 and start from the start i.e. SEEK_SET named constant for the 3rd parameter. Here, we are going to set the pointer to the 10th byte from the start and this api will return -1 if error occurred during setting pointer using lseek() otherwise return the new offset 10 means successfull.
  15. If the lseek() returned -1 then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number.
  16. Now the pointer gets set this is the time to clear ifsdata variable and call the read() api to read the file from the 11th byte upto next 10 bytes by passing parameters as file descriptor, address of ifsdata variable and 10. This will read the content "I DEMO IN" from the whole content "LSEEK C API DEMO IN RPG AS400 PROGRAMMING".
  17. Now for again reading file randomly, call the lseek() api this time with parameters as file descriptor, offset as 10 and start from the current position (20th byte) i.e. SEEK_CUR named constant for the 3rd parameter. Here, we are going to set the pointer to the 30th byte and this api will return -1 if error occurred during setting pointer using lseek() otherwise return the new offset 30 means successfull.
  18. If the lseek() returned -1 then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number.
  19. Now the pointer gets set this is the time to clear ifsdata variable and call the read() api to read the file from the 31st byte upto next 500 bytes(size of the ifsdata variable) by passing parameters as file descriptor, address of ifsdata variable and size of the ifsdata variable. This will read the content "PROGRAMMING" from the whole content "LSEEK C API DEMO IN RPG AS400 PROGRAMMING".
  20. Now for again reading file randomly, call the lseek() api this time with parameters as file descriptor, offset as -10 and start from the end of file (last byte) i.e. SEEK_END named constant for the 3rd parameter. Here, we are going to set the pointer to the 490th byte i.e. (size of data - offset) = 500 - 10 = 490 and this api will return -1 if error occurred during setting pointer using lseek() otherwise return the new offset 490 means successfull.
  21. If the lseek() returned -1 then call the error handling procedures errorIFS() and strerror() respectively to get the error number and the error message corresponding to the error number.
  22. Now the pointer gets set this is the time to clear ifsdata variable and call the read() api to read the file from the 490th byte upto next 10 bytes by passing parameters as file descriptor, address of ifsdata variable and size of the ifsdata variable. This will read the content ""(blank) from the whole content "LSEEK C API DEMO IN RPG AS400 PROGRAMMING" (please note that after PROGRAMMING the data is blank in the ifs stream file upto 200 bytes.

Post a Comment

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