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

Fixed, /Free and Fully Free RPG


Writing bytes to IFS stream files using ifs C api write() in RPGLE

Writing bytes to IFS stream files using write() ifs C api in RPGLE
Writing bytes to IFS stream files using write() ifs C api in RPGLE, write() api, write() procedure in rpgle, write() api in C, write() function in C, write bytes to ifs stream file in rpgle using write() api, isc c api write(), ibmi write() ifs api, as400 write() ifs api, as4000 write() api, how to write data in ifs file using rpgle program, open(), open() api, file descriptor, ifs stream file, writing data to 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,Writing streams with the write() API,The write() API is used to write bytes to a stream file,UNIX-type APIs,C language prototype of write() ,RPG version of the prototype write() api,extproc,Working with the IFS in RPG IV, prototyping of write() api,The path parameter,The oflag parameter,The mode parameter, The codepage parameter,The return value of the open() API,Code snippet showing the use of the open() API,Introduction to the IFS
Writing bytes to IFS stream files using write() ifs C api in RPGLE

There are following steps involved in writing the bytes to a stream file using write() ifs C api. Let's have a look.

  1. Create and Open the IFS stream file by calling open() ifs C api in RPGLE.
  2. Check if file descriptor returned is less than 0, then return from the program.
  3. Otherwise, if file descriptor is greater or equals 0, then pass file descriptor to write() ifs C api with memory area and length to write stream of bytes to the stream file.
  4. Check if number of bytes return from the write() ifs C api is less than expected to write, then its an error and display error message to user.

Please refer to Creating and opening IFS stream files using C API open in RPGLE. for better understanding 1st and 2nd point above.

The write() API

The write() API is used to write bytes to a stream file.

The write api needs to know the area of the memory and length. It copies the bytes from memory to disk. Write() api does not care what data resides in the area of memory. It writes everything present in the memory area.

Prototype of write() in C

int write(int fildes, const void *buf, size_t nbyte);
  1. int: It tells what type of value this write() api returns. The int data type in C is a 32 bits signed integer and is equivalent to "10 I 0" in RPGLE. The write() api returns the number of bytes written to disk. If any error occurs, this number will be less than the number of bytes being asked to write. Therefore, this return value would be used to check for any errors occurred during write to disk.
  2. write:The write() function attempts to write nbyte bytes to the file associated with the open file descriptor
  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.
  4. const void *buf: The "const" is similar to the RPG "const" keyword which means the write() api will not change the content of this parameter. "void *" means pointer can point to anything like character, number, or a data structure of any number of bytes in memory.
  5. size_t nbyte: This is the number of bytes to write. Here, "size_t" is user-defined type. Its defined same variable for which we have used LIKE keyword in RPGLE to define it. The byte size is stored idifferently in different platforms. The size_t is defined inside header file "sys/types.h". In IBM i, size_t is defined as 32 bit unsigned integer i.e. RPG "10U 0".

Prototype for write() API in RPGLE

 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             

Using write() ifs C api in RPGLE program

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

Brief Explanation of the RPGLE code using WRITE() 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 "openfile1" will be created in user, current home directory.
  • Initialize the oflag parameter of the OPEN() api with flags O_createfileifnotexist and O_readwrite which means if file is not present at ifs path then create it first and then open it in read and write 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.
  • If file descriptor is equals or greater than zero then that means file was opened successfully by the open() api, therefor now initialize the data in variable ifsdata.
  • Now call the write() api by passing the file descriptor being returned from the open() api and address of the variable ifsdata and size of the data to be written to disk i.e. ifs stream file. Once write() api got executed it will return us the integer value that tells actual number of bytes being written in ifs.
  • Check if return value from write() api i.e. actual no. of bytes written to disk is lesser than the size of the data expected to be written then it means write() api had some error in writing data to the disk. Therefore, we display error in this situation.
  • 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 from the IBM i command line.
  • For output- IFS file created. Type WRKLNK command on the IBM i command line and press Enter to work with IFS links and look for file named OPENFILE1.
  •  Browse : /home/EASYCLASS/openfile1                                                    
     Record :       1   of       4 by  18                      Column :    1    132 by 131 
     Control :                                                                             
                                                                                           
    ....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+..
     ************Beginning of data**************                                           
    TEST IFS WRITE() API                                                                   
                                                                                           
                                                                                           
                                                                                           
     ************End of Data********************                                           
    

    Post a Comment

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