× 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: Create a stream file in RPG?
  • From: Scott Klement <klemscot@xxxxxxxxxxxx>
  • Date: Wed, 15 Mar 2000 15:55:13 -0600 (CST)

On Wed, 15 Mar 2000, Bale, Dan wrote:

> I searched for "stream file" and did not find any reference in the ILE-RPG
> manuals.  Will I just have to output to a regular flatfile with, say, a
> record length of 500, output one record per print line and use CPYTOSTMF?  


"Stream File" is more of a concept than anything else.    What you 
probably want to search for is "Integrated File System" or "IFS".

You can write data directly to a stream file using the API's for 
IFS that are described in the "Unix-Type API's" manual.   Unfortunately,
I think they only explain how to use the API's in C, not RPG. (but, what
else is new, right?)

The "Who Knew You Could Do That . . ." Redbook does give RPG examples. 
You can find this online at: 
http://www.redbooks.ibm.com/abstracts/sg245402.html


Or, if you want a quick example, heres the same example I always give as
a quick example :)


      * TO COMPILE:
      *  CRTBNDRPG PGM(xxxxx) SRCFILE(xxx/xxxx) DFTACTGRP(*NO) ACTGRP(*NEW)
      *
     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-page
     D O_CODEPAGE      C                   8388608
     D*                                            Open in text-mode
     D O_TEXTDATA      C                   16777216

     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*                                         ascii code-page
     D CP_ASCII        C                   819

     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* 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* 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

     D*--------------------------------------------------------------------
     D* Close a file
     D*
     D* int close(int fildes)
     D*--------------------------------------------------------------------
     D close           PR            10I 0 ExtProc('close')
     D  handle                       10I 0 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 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     close(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     close(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     close(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 ...

Replies:

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.