Closing ifs stream file using ifs C api close() in RPGLE

Closing ifs stream file using ifs C api close() in RPGLE.
Closing ifs stream file using C API close in RPGLE close() API, close(), close api, c api close(), close() api in ifs, close() api in as400, close(0 api in ibmi, Prototype for close() API in RPGLE, int close(int fildes), Using close() ifs C api in RPGLE program, 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 ifs, integrated file system
Closing ifs stream file using C API close in RPGLE 

The close() API

The close() API is just the opposite of the open() api and used to close a file that is being opened using the open() API.

Prototype of close() api in C

int close(int fildes)
  1. int: It tells what type of value this close() api returns. The int data type in C is a 32 bits signed integer and is equivalent to "10 I 0" in RPGLE.
  2. close: It is the name of the procedure.
  3. int fildes:The first parameter is an integer named File descriptor which is the return value from the open() api and identifies the file being opened.

Prototype of close() API in RPGLE

 D close           PR            10i 0 extproc('close')                     *                  
 D fileds                        10i 0 value                                *file descriptor   

It returns a "10I 0" an int if return value is -1 then that means there is some error in closing file using close() api. It accepts one parameter, which is also a 10I 0 an int. The parameter is passed by value, and we use extproc() to call close.

Using close() ifs C api in RPGLE program

RPG Code in Fixed format for Using close() api in RPGLE to close ifs stream file.
     HDFTACTGRP(*NO)                                                                              
     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----&lgt;                                                                          
     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                                   
                                                                             
      *                                                                      
     Dreturn_close     s             10i 0 inz                               
     C                   EVAL      ifspath = '/home/easyclass/openfile2'     
     C                   EVAL      oflag =  O_readwrite +                    
     C                                      O_createfileifnotexist           
     C                   EVAL      mode =  M_executeowner                    
     C                   EVAL      filedescriptor = open(%trim(ifspath):     
     C                             oflag:                                    
     C                             mode)                                     
     C                   IF        filedescriptor < 0                        
     C                   RETURN                                              
     C                   ENDIF                                               
     C                   EVAL      return_close = close(filedescriptor)     
     C                   IF        return_close = -1                        
     C                   RETURN                                             
     C                   ENDIF                                              
     C                   EVAL      *INLR = *ON                              
     C                   RETURN                                                                                      
RPG Code in /Free and /End-Free format for Using close() api in RPGLE to close ifs stream file.
     HDFTACTGRP(*NO)                                                                                
     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                     
      *                                              
     Dreturn_close     s             10i 0 inz       
      /free                                          
       ifspath = '/home/easyclass/openfile2';        
                                                     
       oflag = O_readonly +                          
               O_createfileifnotexist;               
                                                     
       mode  = M_executeowner;                         
                        
       filedescriptor = open(%trim(ifspath):                
                             oflag:                         
                             mode);                         
                                                            
       if filedescriptor < 0;                               
         return;                                            
       endif;                                               
                                                            
       return_close = close(filedescriptor);                
       if return_close = -1;                                
         return;                                            
       endif;                                               
       *inlr = *on;                                         
       return;                                              
      /end-free                                                          
RPG Code in Fully Free format for Using close() api in RPGLE to close ifs stream file.
**FREE                                     
CTL-OPT DFTACTGRP(*NO);                    
DCL-PR close int(10) EXTPROC('close');     
  fileds int(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 return_close int(10) inz;                  
                                                 
       ifspath = '/home/easyclass/openfile2';    
                                                 
       oflag = O_readonly +                      
               O_createfileifnotexist;           
                                                 
       mode  = M_executeowner;                            
                                                 
       filedescriptor = open(%trim(ifspath):     
                             oflag:              
                             mode);              
       if filedescriptor < 0;                    
         return;                                 
       endif;                                    
                                                 
       return_close = close(filedescriptor);     
       if return_close = -1;                     
         return;                                 
       endif;                                    
       *inlr = *on;                              
       return;   

Brief Explanation of the RPGLE code using CLOSE() ifs C api

  • First, we initialize the ifs path variable with the path name for the stream file to be created at IFS. Here, file named "openfile2" will be created in user, current home directory.
  • Initialize the oflag parameter of the OPEN() api with flags O_createfileifnotexist and O_readonly which means if file is not present at ifs path then create it first and then open it in read only mode.
  • Initialize the mode parameter of the OPEN() api with flag M_executeowner which means at the time of file creation provide execute authority to the owner and no authorities to the others/group users.
  • Call the OPEN() api by passing ifspath variable, flag variable and optional mode variable(mode variable is only applicable at the time of file creation if not exist otherwise it is not required to pass. Once the OPEN() api got executed it will return the file descriptor in variable named "filedescriptor".
  • Finally, check whether filedescriptor is less than zero then return from the program which means file was not opened by the OPEN() api and any error occurs during file open. This filedescriptor is associated with the file being opened and is unique for each file being opened by the OPEN() api. It is later used by other IFS C api to know which file has to be operated.
  • Once the file is successfully opened by the open() api, call the close() api and pass the file descriptor being returned from open() api. This will close the file and returned the integer value which tells whether file is closed or not. So, if its return value is -1 that means error closing file.
  • 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- Check the IFS file being created at the specified path using command WRKLNK and in debug mode check the variable value return_value if its not -1 then the file is successfully closed and no error in closing file.
  • Post a Comment

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