× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: RE: Using IFS files
  • From: "Scott Klement" <infosys@xxxxxxxxxxxx>
  • Date: 18 Aug 1999 16:17:42 -0500

Okay...  Venkat, Cary, Rob, and anyone else whos interested,
here is the /COPY member that I use for the IFS functions, along
with a really simple example...

DISCLAIMER:   When I wrote these prototypes I just sat down and
     coded every single prototype :)  I didn't test them all, however!
     open, close, read, write and access (at the very least) work.
     The others SHOULD work, but since I haven't tested them, theres
     a good chance I made a mistake somewhere :)

The /COPY member and the example program are at the bottom of
this message.

Have fun...

Scott Klement
Information Systems Manager
Klement's Sausage Co, Inc.


"Kellems, Cary" <cary.kellems@nissan-usa.com> wrote:
>
> Scott,
>
> Would you also send me your examples?
>
> Thanks
> Cary
>
> > ----------
> > From:  Scott Klement[SMTP:infosys@klements.com]
> > Sent:  Tuesday, August 17, 1999 2:59 PM
> > To:  RPG400-L@midrange.com
> > Subject:  Re: Using IFS files
> >
> > Hi Rob,
> >
> > Rob Berendt <rob@dekko.com> wrote:
> > >
> > > Does anyone have a service program that will open, read and clos
>  an
> > >  IFS
> > > file?  I've used other API's but those Unix API's are documented
> > >  totally
> > > differently.
> > >
> >
> > I don't use a service program for this, usually, instead I just us
> > a /COPY member that includes the various prototypes that I'd need
> > for accessing IFS functions.
> >
> > Then, I just use the IFS functions in my program directly.
> >
> > I'll agree with you that the Unix-type API's manual is completely
> > useless if you don't know C.  However, since I know C as well as
> > RPG, I find it quite useful :)
> >
> > Are you interested in a non-service program example?
> >
> > Scott Klement
> > Information Systems Manager
> > Klement's Sausage Co, Inc.



Here's the /COPY member.  I call the member IFSIO_H
----------------------------------------------------------------------

 *  This header file contains the constants, structures and prototypes
 *  for using the Integrated File System API
 *
 *  These APIs were designed originally for use in C programming.
 *  Therefore bear in mind:
 *   1) All strings must be null-terminated, and variable-length.
 *   2) You must bind to the ILE C binding directory QC2LE
 *   3) Errors are returned in the errno variable, and strings for
 *         them are available by calling strerror()
 *
 *                                               SCK 03/24/1999

 *
 *  To use these in your source code, you need a D-spec like this:
 *  D/COPY LIBSOR/QRPGLESRC,IFSIO_H

 *  For detailed info seee the UNIX-type APIs manual


 * This header file includes (in order)
 *   1) Constants
 *   2) Structures
 *   3) Prototypes


D*                                         ascii code-page
D CP_ASCII        C                   819

D*********************************************************************
D*  Flags for use in open()
D*
D* More than one can be used -- add them together.
D*********************************************************************
D*                                            Reading Only
D O_RDONLY        C                   1
D*                                            Writing Only
D O_WRONLY        C                   2
D*                                            Reading & Writing
D O_RDWR          C                   4
D*                                            Create File if not exist
D O_CREAT         C                   8
D*                                            Exclusively create
D O_EXCL          C                   16
D*                                            Truncate File to 0 bytes
D O_TRUNC         C                   64
D*                                            Append to File
D O_APPEND        C                   256
D*                                            Convert text by code-pag
D O_CODEPAGE      C                   8388608
D*                                            Open in text-mode
D O_TEXTDATA      C                   16777216

D*********************************************************************
D* Access mode flags for access()
D*********************************************************************
D F_OK            C                   0
D R_OK            C                   4
D W_OK            C                   2
D X_OK            C                   1

D*********************************************************************
D*      Mode Flags.
D*         basically, the mode parm of open(), creat(), chmod(),etc
D*         uses 9 least significant bits to determine the
D*         file's mode. (peoples access rights to the file)
D*
D*           user:       owner    group    other
D*           access:     R W X    R W X    R W X
D*           bit:        8 7 6    5 4 3    2 1 0
D*
D* (This is accomplished by adding the flags below to get the mode)
D*********************************************************************
D*                                         owner authority
D S_IRUSR         C                   256
D S_IWUSR         C                   128
D S_IXUSR         C                   64
D S_IRWXU         C                   448
D*                                         group authority
D S_IRGRP         C                   32
D S_IWGRP         C                   16
D S_IXGRP         C                   8
D S_IRWXG         C                   56
D*                                         other people
D S_IROTH         C                   4
D S_IWOTH         C                   2
D S_IXOTH         C                   1
D S_IRWXO         C                   7

D*********************************************************************
D* "whence" constants for use with lseek()
D*********************************************************************
D SEEK_SET        C                   CONST(0)
D SEEK_CUR        C                   CONST(1)
D SEEK_END        C                   CONST(2)


D*********************************************************************
D* File Information Structure (stat)
D*
D* struct stat {
D*  mode_t         st_mode;       /* File mode                       *
D*  ino_t          st_ino;        /* File serial number              *
D*  nlink_t        st_nlink;      /* Number of links                 *
D*  uid_t          st_uid;        /* User ID of the owner of file    *
D*  gid_t          st_gid;        /* Group ID of the group of file   *
D*  off_t          st_size;       /* For regular files, the file
D*                                 * size in bytes                   *
D*  time_t         st_atime;      /* Time of last access             *
D*  time_t         st_mtime;      /* Time of last data modification  *
D*  time_t         st_ctime;      /* Time of last file status change *
D*  dev_t          st_dev;        /* ID of device containing file    *
D*  size_t         st_blksize;    /* Size of a block of the file     *
D*  unsigned long  st_allocsize;  /* Allocation size of the file     *
D*  qp0l_objtype_t st_objtype;    /* AS/400 object type              *
D*  unsigned short st_codepage;   /* Object data codepage            *
D*  char           st_reserved1[66]; /* Reserved                     *
D* };
D*
D p_statds        S               *
D statds          DS                  BASED(p_statds)
D   st_mode                     10U 0
D   st_ino                      10U 0
D   st_nlink                     5U 0
D   st_pad                       2A
D   st_uid                      10U 0
D   st_gid                      10U 0
D   st_size                     10I 0
D   st_atime                    10I 0
D   st_mtime                    10I 0
D   st_ctime                    10I 0
D   st_dev                      10U 0
D   st_blksize                  10U 0
D   st_alctize                  10U 0
D   st_objtype                  12A
D   st_codepag                   5U 0
D   st_resv11                   67A


D*********************************************************************
D* Group Information Structure (group)
D*
D*  struct group {
D*        char    *gr_name;        /* Group name.
D*        gid_t   gr_gid;          /* Group id.
D*        char    **gr_mem;        /* A null-terminated list of pointe
D*                                    to the individual member names.
D*  };
D*
D p_group         S               *
D group           DS                  Based(p_group)
D   gr_name                       *
D   gr_gid                      10U 0
D   gr_mem                        *   DIM(256)


D*********************************************************************
D*
D* User Information Structure (passwd)
D*
D* (Don't let the name fool you, this structure does not contain
D*  any password information.  Its named after the UNIX file that
D*  contains all of the user info.  That file is "passwd")
D*
D*   struct passwd {
D*        char    *pw_name;            /* User name.
D*        uid_t   pw_uid;              /* User ID number.
D*        gid_t   pw_gid;              /* Group ID number.
D*        char    *pw_dir;             /* Initial working directory.
D*        char    *pw_shell;           /* Initial user program.
D*   };
D*
D p_passwd        S               *
D passwd          DS                  BASED(p_passwd)
D  pw_name                        *
D  pw_uid                       10U 0
D  pw_gid                       10U 0
D  pw_dir                         *
D  pw_shell                       *


D*********************************************************************
D* File Time Structure (utimbuf)
D*
D* struct utimbuf {
D*    time_t     actime;           /*  access time       */
D*    time_t     modtime;          /*  modification time */
D* };
D*
D p_utimbuf       S               *
D utimbuf         DS                  based(p_utimbuf)
D   actime                      10I 0
D   modtime                     10I 0


D*********************************************************************
D*
D* Directory Entry Structure (dirent)
D*
D* struct dirent {
D*   char           d_reserved1[16];  /* Reserved
D*   unsigned int   d_reserved2;      /* Reserved
D*   ino_t          d_fileno;         /* The file number of the file
D*   unsigned int   d_reclen;         /* Length of this directory entr
D*                                     * in bytes
D*   int            d_reserved3;      /* Reserved
D*   char           d_reserved4[8];   /* Reserved
D*   qlg_nls_t      d_nlsinfo;        /* National Language Information
D*                                     * about d_name
D*   unsigned int   d_namelen;        /* Length of the name, in bytes
D*                                     * excluding NULL terminator
D*   char           d_name[_QP0L_DIR_NAME]; /* Name...null terminated
D*
D* };
D*
D p_dirent        s               *
D dirent          ds                  based(p_dirent)
D   d_reserv1                   16A
D   d_reserv2                   10U 0
D   d_fileno                    10U 0
D   d_reclen                    10U 0
D   d_reserv3                   10I 0
D   d_reserv4                    8A
D   d_nlsinfo                   12A
D     nls_ccsid                 10I 0 OVERLAY(d_nlsinfo:1)
D     nls_cntry                  2A   OVERLAY(d_nlsinfo:5)
D     nls_lang                   3A   OVERLAY(d_nlsinfo:7)
D     nls_reserv                 3A   OVERLAY(d_nlsinfo:10)
D   d_namelen                   10U 0
D   d_name                     640A



D*--------------------------------------------------------------------
D* Determine file accessibility
D*
D* int access(const char *path, int amode)
D*
D*--------------------------------------------------------------------
D access          PR            10I 0 ExtProc('access')
D   Path                          *   Value
D   amode                       10I 0 Value

D*--------------------------------------------------------------------
D* Change Directory
D*
D* int chdir(const char *path)
D*--------------------------------------------------------------------
D chdir           PR            10I 0 ExtProc('chdir')
D   path                          *   Value

D*--------------------------------------------------------------------
D* Change file authorizations
D*
D* int chmod(const char *path, mode_t mode)
D*--------------------------------------------------------------------
D chmod           PR            10I 0 ExtProc('chmod')
D   path                          *   Value
D   mode                        10U 0 Value

D*--------------------------------------------------------------------
D* Change Owner/Group of File
D*
D* int chown(const char *path, uid_t owner, gid_t group)
D*--------------------------------------------------------------------
D chown           PR            10I 0 ExtProc('chown')
D   path                          *   Value
D   owner                       10U 0 Value
D   group                       10U 0 Value

D*--------------------------------------------------------------------
D* Close a file
D*
D* int close(int fildes)
D*--------------------------------------------------------------------
D closef          PR            10I 0 ExtProc('close')
D  handle                       10I 0 value

D*--------------------------------------------------------------------
D* Close a directory
D*
D* int closedir(DIR *dirp)
D*--------------------------------------------------------------------
D closedir        PR            10I 0 EXTPROC('closedir')
D  dirhandle                      *   VALUE

D*--------------------------------------------------------------------
D* Create or Rewrite File
D*
D* int creat(const char *path, mode_t mode)
D*--------------------------------------------------------------------
D creat           PR            10I 0 ExtProc('creat')
D   path                          *   Value
D   mode                        10U 0 Value

D*--------------------------------------------------------------------
D* Duplicate open file descriptor
D*
D* int dup(int fildes)
D*--------------------------------------------------------------------
D dup             PR            10I 0 ExtProc('dup')
D   fildes                      10I 0 Value

D*--------------------------------------------------------------------
D* Duplicate open file descriptor to another descriptor
D*
D* int dup2(int fildes, int fildes2)
D*--------------------------------------------------------------------
D dup2            PR            10I 0 ExtProc('dup2')
D   fildes                      10I 0 Value
D   fildes2                     10I 0 Value

D*--------------------------------------------------------------------
D* Change file authorizations by descriptor
D*
D* int fchmod(int fildes, mode_t mode)
D*--------------------------------------------------------------------
D fchmod          PR            10I 0 ExtProc('fchmod')
D   fildes                      10I 0 Value
D   mode                        10U 0 Value

D*--------------------------------------------------------------------
D* Change Owner and Group of File by Descriptor
D*
D* int fchown(int fildes, uid_t owner, gid_t group)
D*--------------------------------------------------------------------
D fchown          PR            10I 0 ExtProc('fchown')
D   fildes                      10I 0 Value
D   owner                       10U 0 Value
D   group                       10U 0 Value

D*--------------------------------------------------------------------
D* Perform File Control
D*
D* int fcntl(int fildes, int cmd, . . .)
D*--------------------------------------------------------------------
D fcntl           PR            10I 0 ExtProc('fcntl')
D   fildes                      10I 0 Value
D   cmd                         10I 0 Value
D   arg                         10I 0 Value options(*nopass)

D*--------------------------------------------------------------------
D* Get configurable path name variables by descriptor
D*
D* long fpathconf(int fildes, int name)
D*--------------------------------------------------------------------
D fpathconf       PR            10I 0 ExtProc('fpathconf')
D   fildes                      10I 0 Value
D   name                        10I 0 Value

D*--------------------------------------------------------------------
D* Get File Information by Descriptor
D*
D* int fstat(int fildes, struct stat *buf)
D*--------------------------------------------------------------------
D fstat           PR            10I 0 ExtProc('fstat')
D   fildes                      10I 0 Value
D   buf                           *   Value

D*--------------------------------------------------------------------
D* Synchronize Changes to fIle
D*
D* int fsync(int fildes)
D*--------------------------------------------------------------------
D fsync           PR            10I 0 ExtProc('fsync')
D   fildes                      10I 0 Value

D*--------------------------------------------------------------------
D* Truncate file
D*
D* int ftruncate(int fildes, off_t length)
D*--------------------------------------------------------------------
D ftruncate       PR            10I 0 ExtProc('ftruncate')
D   fildes                      10I 0 Value
D   length                      10I 0 Value

D*--------------------------------------------------------------------
D* Get current working directory
D*
D* char *getcwd(char *buf, size_t size)
D*--------------------------------------------------------------------
D getcwd          PR              *   ExtProc('getcwd')
D   buf                           *   Value
D   size                        10U 0 Value

D*--------------------------------------------------------------------
D* Get effective group ID
D*
D* gid_t getegid(void)
D*--------------------------------------------------------------------
D getegid         PR            10U 0 ExtProc('getegid')

D*--------------------------------------------------------------------
D* Get effective user ID
D*
D* uid_t geteuid(void)
D*--------------------------------------------------------------------
D geteuid         PR            10U 0 ExtProc('geteuid')

D*--------------------------------------------------------------------
D* Get Real Group ID
D*
D* gid_t getgid(void)
D*--------------------------------------------------------------------
D getgid          PR            10U 0 ExtProc('getgid')

D*--------------------------------------------------------------------
D* Get group information from group ID
D*
D* struct group *getgrgid(gid_t gid)
D*--------------------------------------------------------------------
D getgrid         PR              *   ExtProc('getgrgid')
D   gid                         10U 0 VALUE

D*--------------------------------------------------------------------
D* Get group info using group name
D*
D* struct group  *getgrnam(const char *name)
D*--------------------------------------------------------------------
D getgrnam        PR              *   ExtProc('getgrnam')
D   name                          *   VALUE

D*--------------------------------------------------------------------
D* Get group IDs
D*
D* int getgroups(int gidsetsize, gid_t grouplist[])
D*--------------------------------------------------------------------
D getgroups       PR              *   ExtProc('getgroups')
D   gidsetsize                  10I 0 VALUE
D   grouplist                     *

D*--------------------------------------------------------------------
D* Get user information by user-name
D*
D* (Don't let the name mislead you, this does not return the password,
D*  the user info database on unix systems is called "passwd",
D*  therefore, getting the user info is called "getpw")
D*
D* struct passwd *getpwnam(const char *name)
D*--------------------------------------------------------------------
D getpwnam        PR              *   ExtProc('getpwnam')
D   name                          *   Value

D*--------------------------------------------------------------------
D* Get user information by user-id
D*
D* (Don't let the name mislead you, this does not return the password,
D*  the user info database on unix systems is called "passwd",
D*  therefore, getting the user info is called "getpw")
D*
D* struct passwd *getpwuid(uid_t uid)
D*--------------------------------------------------------------------
D getpwuid        PR              *   extproc('getpwuid')
D   uid                         10U 0 Value

D*--------------------------------------------------------------------
D* Get Real User-ID
D*
D* uid_t getuid(void)
D*--------------------------------------------------------------------
D getuid          PR            10U 0 ExtProc('getuid')

D*--------------------------------------------------------------------
D* Perform I/O Control Request
D*
D* int ioctl(int fildes, unsigned long req, ...)
D*--------------------------------------------------------------------
D ioctl           PR            10I 0 ExtProc('ioctl')
D   fildes                      10I 0 Value
D   req                         10U 0 Value
D   arg                           *   Value

D*--------------------------------------------------------------------
D* Create Link to File
D*
D* int link(const char *existing, const char *new)
D*--------------------------------------------------------------------
D link            PR            10I 0 ExtProc('link')
D   existing                      *   Value
D   new                           *   Value

D*--------------------------------------------------------------------
D* Set File Read/Write Offset
D*
D* off_t lseek(int fildes, off_t offset, int whence)
D*--------------------------------------------------------------------
D lseek           PR            10I 0 ExtProc('lseek')
D   fildes                      10I 0 value
D   offset                      10I 0 value
D   whence                      10I 0 value

D*--------------------------------------------------------------------
D* Get File or Link Information
D*
D* int lstat(const char *path, struct stat *buf)
D*--------------------------------------------------------------------
D lstat           PR            10I 0 ExtProc('lstat')
D   path                          *   Value
D   buf                           *   Value

D*--------------------------------------------------------------------
D* Make Directory
D*
D* int mkdir(const char *path, mode_t mode)
D*--------------------------------------------------------------------
D mkdir           PR            10I 0 ExtProc('mkdir')
D   path                          *   Value
D   mode                        10U 0 Value

D*--------------------------------------------------------------------
D* Open a File
D*
D* int open(const char *path, int oflag, . . .);
D*--------------------------------------------------------------------
D open            PR            10I 0 ExtProc('open')
D  filename                       *   value
D  openflags                    10I 0 value
D  mode                         10U 0 value options(*nopass)
D  codepage                     10U 0 value options(*nopass)

D*--------------------------------------------------------------------
D* Open a Directory
D*
D* DIR *opendir(const char *dirname)
D*--------------------------------------------------------------------
D opendir         PR              *   EXTPROC('opendir')
D  dirname                        *   VALUE

D*--------------------------------------------------------------------
D* Get configurable path name variables
D*
D* long pathconf(const char *path, int name)
D*--------------------------------------------------------------------
D pathconf        PR            10I 0 ExtProc('pathconf')
D   path                          *   Value
D   name                        10I 0 Value

D*--------------------------------------------------------------------
D* Get path name of object from its file id
D*
D* char *Qp0lGetPathFromFileID(char *buf, size_t size,Qp0lFID_t fileid
D*--------------------------------------------------------------------
D GetPathFID      PR              *   ExtProc('Qp0lGetPathFromFileID')
D   buf                           *   Value
D   size                        10U 0 Value
D   fileid                      16A

D*--------------------------------------------------------------------
D* Rename File or Directory, return error if a file/dir under the
D*   "new" name already exists.
D*
D* int Qp0lRenameKeep(const char *old, const char *new)
D*--------------------------------------------------------------------
D Rename          PR            10I 0 ExtProc('Qp0lRenameKeep')
D   old                           *   Value
D   new                           *   Value

D*--------------------------------------------------------------------
D* Rename File or Directory.  If another file/dir exists under the
D*    "new" name, delete it first.
D*
D* int Qp0lRenameUnlink(const char *old, const char *new)
D*--------------------------------------------------------------------
D Replace         PR            10I 0 ExtProc('Qp0lRenameUnlink')
D   old                           *   Value
D   new                           *   Value

D*--------------------------------------------------------------------
D* Read From a File
D*
D* ssize_t read(int handle, void *buffer, size_t bytes);
D*--------------------------------------------------------------------
D read            PR            10I 0 ExtProc('read')
D  handle                       10i 0 value
D  buffer                         *   value
D  bytes                        10U 0 value

D*--------------------------------------------------------------------
D* Read Directory Entry
D*
D* struct dirent *readdir(DIR *dirp)
D*--------------------------------------------------------------------
D readdir         PR              *   EXTPROC('readdir')
D  dirp                           *   VALUE

D*--------------------------------------------------------------------
D* Read Value of Symbolic Link
D*
D* int readlink(const char *path, char *buf, size_t bufsiz)
D*--------------------------------------------------------------------
D readlink        PR            10I 0 ExtProc('readlink')
D   path                          *   value
D   buf                           *   value
D   bufsiz                      10U 0 value

D*--------------------------------------------------------------------
D* Reset Directory Stream to Beginning
D*
D* void rewinddir(DIR *dirp)
D*--------------------------------------------------------------------
D rewinddir       PR                  ExtProc('rewinddir')
D   dirp                          *   value


D*--------------------------------------------------------------------
D* Remove Directory
D*
D* int rmdir(const char *path)
D*--------------------------------------------------------------------
D rmdir           PR            10I 0 ExtProc('rmdir')
D   path                          *   value

D*--------------------------------------------------------------------
D* Get File Information
D*
D* int stat(const char *path, struct stat *buf)
D*--------------------------------------------------------------------
D stat            PR            10I 0 ExtProc('stat')
D   path                          *   value
D   buf                           *   value

D*--------------------------------------------------------------------
D* Make Symbolic Link
D*
D* int symlink(const char *pname, const char *slink)
D*--------------------------------------------------------------------
D symlink         PR            10I 0 ExtProc('symlink')
D   pname                         *   value
D   slink                         *   value

D*--------------------------------------------------------------------
D* Get system configuration variables
D*
D* long sysconf(int name)
D*--------------------------------------------------------------------
D sysconf         PR            10I 0 ExtProc('sysconf')
D   name                        10I 0 Value

D*--------------------------------------------------------------------
D* Set Authorization Mask for Job
D*
D* mode_t umask(mode_t cmask)
D*--------------------------------------------------------------------
D umask           PR            10U 0 ExtProc('umask')
D   cmask                       10U 0 Value

D*--------------------------------------------------------------------
D* Remove Link to File.  (Deletes Directory Entry for File, and if
D*    this was the last link to the file data, the file itself is
D*    also deleted)
D*
D* int unlink(const char *path)
D*--------------------------------------------------------------------
D unlink          PR            10I 0 ExtProc('unlink')
D   path                          *   Value

D*--------------------------------------------------------------------
D* Set File Access & Modification Times
D*
D* int utime(const char *path, const struct utimbuf *times)
D*--------------------------------------------------------------------
D utime           PR            10I 0 ExtProc('utime')
D   path                          *   value
D   times                         *   value

D*--------------------------------------------------------------------
D* Write to a file
D*
D* ssize_t write(int fildes, const void *buf, size_t bytes)
D*--------------------------------------------------------------------
D write           PR            10I 0 ExtProc('write')
D  handle                       10I 0 value
D  buffer                         *   value
D  bytes                        10U 0 value



Here's an example program (RPG IV at V3R2) that uses these things...
----------------------------------------------------------------------

D/COPY QRPGLESRC,IFSIO_H

D filename        S             64A
D p_filename      S               *   INZ(%addr(filename))
D oflag           S             10U 0
D omode           S             10U 0
D buffer          S            128A
D p_Buffer        s               *   INZ(%addr(buffer))
D buflen          S             10U 0
D fd              S             10I 0

c                   exsr      MakeFile
c                   exsr      WriteFile
c                   exsr      ReadFile

c                   eval      *INLR = *On


C*===============================================================
C* This creates a file with an ASCII codepage.
C*
C* By creating it with a codepage, and then closing it
C* again, we assign it a codepage.
C*
C* Next time we open it, we can use O_TEXTDATA which will
C* make it automatically translate from this jobs codepage
C* (whatever it happens to be) to ASCII.
C*===============================================================
csr   MakeFile      begsr
C*-----------------------
C* make filename
c                   eval      filename = '/QOpenSys/test.file' + x'00'
C* set open flags to "create" for "read/write" with a codepage.
c                   eval      oflag = O_CREAT+O_RDWR + O_CODEPAGE
C* set mode to User=RW, Group=R, Other=R  (RW-R--R--)
c                   eval      omode = S_IRUSR+S_IWUSR+S_IRGRP+S_IROTH

c                   eval      fd = open(p_filename: oflag: omode:
c                                      CP_ASCII)
c                   if        fd < 0
c                   eval      Msg = 'Cant create file!'
c                   dsply                   Msg              50
c                   eval      *INLR = *On
c                   Return
c                   endif

c                   callp     closef(fd)
C*-----------------------
csr                 endsr


C*===============================================================
C* This opens the file and writes some data into it.
C*
C* We use O_TEXTDATA when we open the file so it will translate
C* whatever we write to whichever codepage was assigned to the
C* file when it was created...
C*
C* Note that we use x'0D25' which when translated to ASCII will
C*  become CR/LF, the correct end-of-line for a DOS/Windows file
C*===============================================================
csr   WriteFile     begsr
C*-----------------------
c                   eval      fd = open(p_filename: O_RDWR+O_TEXTDATA)
c                   if        fd < 0
c                   eval      Msg = 'Cant open file to write to it.'
c                   dsply                   Msg
c                   eval      *INLR = *On
c                   Return
c                   endif

c                   eval      Buffer = 'This data will be written as -
c                             ASCII data.' + x'0D25'
c     ' '           checkr    Buffer        BufLen
c                   if        write(fd: p_buffer: BufLen) < 0
c                   eval      Msg = 'write() failed!'
c                   dsply                   Msg
c                   eval      *INLR = *On
c                   Return
c                   endif

c                   callp     closef(fd)
C*-----------------------
csr                 endsr


C*===============================================================
C* This opens the file and reads the data inside.
C*
C* Again, we use O_TEXTDATA to convert the data to our codepage
C*
C* Before displaying the text, we strip off the end-of-line stuff
C*===============================================================
csr   ReadFile      begsr
C*-----------------------
c                   eval      fd = open(p_filename: O_RDWR+O_TEXTDATA)
c                   if        fd < 0
c                   eval      Msg = 'Cant open file to read from it.'
c                   dsply                   Msg
c                   eval      *INLR = *On
c                   Return
c                   endif

c                   eval      buffer = *blanks

c                   if        read(fd: p_buffer: BufLen) < 0
c                   eval      Msg = 'read() failed!'
c                   dsply                   Msg
c                   eval      *INLR = *On
c                   Return
c                   endif

c     x'0D25'       scan      Buffer        pos               2 0
c                   eval      Msg = %subst(Buffer:1:pos-1)
c                   dsply                   Msg

c                   callp     closef(fd)
C*-----------------------
csr                 endsr


+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.