Reading bytes from IFS stream file using C API read in RPGLE |
The read() API
The read() API is just the opposite of the write() API. It reads bytes of data from a IFS stream file, and stores them into the area of memory(buffer) that we point it to.
the read() function reads n number of bytes of input into the memory area indicated by buffer.
Prototype of read() api in C
int read(int fildes, void *buf, size_t nbyte);
- int: It tells what type of value this read() api returns. The int data type in C is a 32 bits signed integer and is equivalent to "10 I 0" in RPGLE. It returns the actual number of bytes being read than asked to read.
- the read() function reads nbyte bytes of input into the memory area indicated by buf.
- 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.
- void *buf:read() api will store bytes in buf that it loads from the stream file being read. This is a void pointer which means any no. of bytes can be read and any type of data can be loaded into buf whether its character, numeric or data structure.
- size_t nbyte: This is the number of bytes to read. The read() api will read the no. of byets specified in nbytes paramater from the stream file. However, if we are at the end of the stream file we may end up in reading fewer bytes than expected by the read() api. 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". If nbyte is zero, read() returns a value of zero without attempting any other action.
Prototype for read() API in RPGLE
RPG Code in Fixed format for Using read() api in RPGLE to read bytes from IFS stream files.
HDFTACTGRP(*NO) 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 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 Dreturn_read s 10i 0 inz C EVAL ifspath = '/home/easyclass/openfile1' C EVAL oflag = O_readonly C EVAL filedescriptor = open(%trim(ifspath): C oflag) C IF filedescriptor < 0 C RETURN C ENDIF C EVAL return_read = read(filedescriptor: C %addr(ifsdata):%size(ifsdata)) C IF return_read < 1 C 'EOF' DSPLY C ENDIF C EVAL *INLR = *ON C RETURN
RPG Code in /Free and /End-Free format for Using read() api in RPGLE to read bytes from IFS stream files.
HDFTACTGRP(*NO) 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 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 Dreturn_read s 10i 0 inz /free ifspath = '/home/easyclass/openfile1'; oflag = O_readonly ; filedescriptor = open(%trim(ifspath): oflag); if filedescriptor < 0; return; endif; return_read = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); if return_read < 1; DSPLY 'EOF'; endif; *inlr = *on; return; /end-free
RPG Code in Fully Free format for Using read() api in RPGLE to read bytes from IFS stream files.
**FREE CTL-OPT DFTACTGRP(*NO); DCL-PR read int(10) EXTPROC('read'); fileds int(10) VALUE; buffer pointer VALUE; noofbytes uns(10) VALUE; END-PR; DCL-PR open int(10) EXTPROC('open'); ifspath pointer VALUE options(*string); oflag int(10) VALUE; mode uns(10) VALUE options(*nopass); codepage uns(10) VALUE options(*nopass); END-PR; // * <-----oflag----> DCL-C O_readonly 1; DCL-C O_writeonly 2; DCL-C O_readwrite 4; DCL-C O_createfileifnotexist 8; DCL-C O_exclusivecreate 16; DCL-C O_truncateto0bytes 64; DCL-C O_appendtofile 256; DCL-C O_converttextbycodepage 8388608; DCL-C O_openintextmode 16777216; // * <-----mode----> // * owner,group,other (RWX) // * owner authority DCL-C M_readowner 256; DCL-C M_writeowner 128; DCL-C M_executeowner 64; // * group authority DCL-C M_readgroup 32; DCL-C M_writegroup 16; DCL-C M_executegroup 8; // * other people DCL-C M_readother 4; DCL-C M_writeother 2; DCL-C M_executeother 1; DCL-S ifspath CHAR(512); DCL-S oflag int(10); DCL-S mode uns(10); DCL-S codepage uns(10); DCL-S filedescriptor int(10); DCL-S ifsdata char(500) inz; DCL-S return_read int(10) inz; ifspath = '/home/easyclass/openfile1'; oflag = O_readonly ; filedescriptor = open(%trim(ifspath): oflag); if filedescriptor < 0; return; endif; return_read = read(filedescriptor:%addr(ifsdata):%size(ifsdata)); if return_read < 1; DSPLY 'EOF'; endif; *inlr = *on; return;