2.8. Example of writing raw data to a stream file

As I've mentioned in previous sections, stream files can be used for any byte values, not just for words and other text. As a proof of concept, I thought it might be fun to generate a very small MS-DOS program as a stream file.

Programs under OS/400 are stored in *PGM objects. You can't directly open and manipulate a *PGM object on the AS/400. You have to write source code, and let the compiler compile it.

However, on the PC, all objects are stored as stream files. It doesn't matter if it's a data file, a program, an audio file, etc. Every object is stored as a stream file!

To prove that, here's an RPG program that actually generates a PC program. The stream file that it outputs can be downloaded to your PC, and run. (Provided, of course, that the PC is able to run MS-DOS programs).

      * CH2RAWDTA: Example of writing non-text to a stream file
      *  (From Chap 2)
      *
      * To compile:
      *   CRTBNDRPG CH2RAWDTA SRCFILE(xxx/QRPGLESRC) DBGVIEW(*LIST)
      *
     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')

     D/copy IFSEBOOK/QRPGLESRC,IFSIO_H
     D/copy IFSEBOOK/QRPGLESRC,ERRNO_H

     D fd              S             10I 0
     D wrdata          S             79A
     D err             S             10I 0

     c                   eval      wrdata = x'B409BA0C01CD21B8004CCD21' +
     c                                      x'416C6C206F626A6563747320' +
     c                                      x'6F6E20746865205043206172' +
     c                                      x'652073746F72656420696E20' +
     c                                      x'2273747265616D2066696C65' +
     c                                      x'73222C206576656E2070726F' +
     c                                      x'6772616D732124'

     c                   eval      fd = open('/ifstest/littlepgm.com':
     c                                       O_WRONLY+O_CREAT+O_TRUNC:
     c                                       S_IRUSR + S_IWUSR + S_IXUSR
     c                                     + S_IRGRP + S_IXGRP
     c                                     + S_IROTH + S_IXOTH)
     c                   if        fd < 0
     c                   callp     EscErrno(errno)
     c                   endif

     c                   if        write(fd: %addr(wrdata): %size(wrdata))
     c                                  < %size(wrdata)
     c                   eval      err = errno
     c                   callp     close(fd)
     c                   callp     EscErrno(err)
     c                   endif

     c                   callp     close(fd)

     c                   eval      *inlr = *on
     c                   return

      /DEFINE ERRNO_LOAD_PROCEDURE
      /COPY IFSEBOOK/QRPGLESRC,ERRNO_H

This program outputs a stream file called littlepgm.com into our /ifstest directory. To run this program you'll need to transfer it to your PC. One way to do this, if your system is set up for it, would be to map a network drive to your AS/400. Another good choice would be to FTP the file to your PC. If you decide to use FTP, make sure that you use binary mode.

Once you've got it on your PC, you should run the program from an MS-DOS prompt.