2.5. Example of writing and reading data to a stream file

The last few topics have probably given you lots of new things to think about. It's time to play with them!

First of all, let's create a directory to write all of our stream files to. This will keep us from cluttering up the root directory of the IFS with our tests. Let's call this new directory "ifstest." We can create it by typing the following command at our OS/400 command-line:

CRTDIR DIR('/ifstest')

If you get an error that you do not have sufficient authority to create it, or something like that, you may need to speak with your system administrator. Tell him that you need a sandbox to play in!

Here's a program which both writes and reads data from a stream file. It also demonstrates one of the properties of a stream file -- the data is stored as a continuous stream of bytes, not in records.

Take a look at it, guess what you think it does, then try it out. It's pretty cool!

      * CH2WRRD: Example of writing & reading data to a stream file
      *  (From Chap 2)
      *
      * To compile:
      *   CRTBNDRPG CH2WRRD SRCFILE(xxx/QRPGLESRC) DBGVIEW(*LIST)
      *
     H DFTACTGRP(*NO) ACTGRP(*NEW)

     D/copy IFSEBOOK/QRPGLESRC,IFSIO_H

     D fd              S             10I 0
     D wrdata          S             24A
     D rddata          S             48A
     D flags           S             10U 0
     D mode            S             10U 0
     D Msg             S             50A
     D Len             S             10I 0

     C****************************************************************
     C* Example of writing data to a stream file
     C****************************************************************
     c                   eval      flags = O_WRONLY + O_CREAT + O_TRUNC

     c                   eval      mode =  S_IRUSR + S_IWUSR
     c                                   + S_IRGRP
     c                                   + S_IROTH

     c                   eval      fd = open('/ifstest/ch2_test.dat':
     c                                       flags: mode)
     c                   if        fd < 0
     c                   eval      Msg = 'open(): failed for writing'
     c                   dsply                   Msg
     c                   eval      *inlr = *on
     c                   return
     c                   endif

     C* Write some data
     c                   eval      wrdata = 'THE QUICK BROWN FOX JUMP'
     c                   callp     write(fd: %addr(wrdata): %size(wrdata))

     C* Write some more data
     c                   eval      wrdata = 'ED OVER THE LAZY GIRAFFE'
     c                   callp     write(fd: %addr(wrdata): %size(wrdata))

     C* close the file
     c                   callp     close(fd)

     C****************************************************************
     C* Example of reading data from a stream file
     C****************************************************************
     c                   eval      flags = O_RDONLY

     c                   eval      fd = open('/ifstest/ch2_test.dat':
     c                                       flags)
     c                   if        fd < 0
     c                   eval      Msg = 'open(): failed for reading'
     c                   dsply                   Msg
     c                   eval      *inlr = *on
     c                   return
     c                   endif

     c                   eval      len = read(fd: %addr(rddata):
     c                                            %size(rddata))
     c                   eval      Msg = 'Length read = ' +
     c                                  %trim(%editc(len:'M'))
     c     Msg           dsply
     c                   dsply                   rddata

     c                   callp     close(fd)

     c                   eval      *inlr = *on
     c                   return

As before, there are instructions on compiling the program near the top of the source. Give it a try. I'll be right back, I'm going to go get a cold beverage.

Ahhhh... lime soda.