Logo2
IFS
[Home] [Code Samples] [IFS]
WRKLNK0103
WRKLNK0203

Writing to the IFS

based upon code written by: Scott Klement

This program, based on Scott Klement's work, writes records to the IFS.  

Can it be more complicated?  Sure.  But this is a simplified starting place 
that will work to get you started.

The directory and file may be viewed with the WRKLNK command and Option 5 
to navigate to the directory and file. 

You will want to change the directory and file to names of your choosing, 
and of course add your own method of populating the file with real data.

You may choose to add records to an existing file (APPEND) or replace the 
existing records (TRUNC).  If the directory and/or file do not exist they 
will be created.

This program can be copy & pasted, and will run as-is so that you can see 
if it works on your own machine: 
	WITH THIS ONE CHANGE: 
		Change the cDirPath &
		change the cFileName	!!
                                   





The RPG to write a file to the IFS

      //*********************************************************************
      //     ___             _    _     __ __             _    _            *
      //    | . > ___  ___ _| |_ | |_  |  \  \ ___  _ _ _| |_ <_>._ _       *
      //    | . \/ . \/ . \ | |  | . | |     |<_> || '_> | |  | || ' |      *
      //    |___/\___/\___/ |_|  |_|_| |_|_|_|<___||_|   |_|  |_||_|_|      *
      //                                                                    *
      //    A program to write a report to the IFS                          *
      //                                                                    *
      //     IFSFILER                                                       *
      //                                                                    *
      //     12/2012                                     booth@martinvt.com *
      //*********************************************************************
      // COMMENTS     -= This all comes from Scott Klement =-               *
      //              =======================================               *
      //                                                                    *
      //        -  cDirPath is your name for your desired directory.        *
      //           cFileName is your name for your desired file.            *
      //           Be sure to change them to names meaningful to you.       *
      //                                                                    *
      //        - The lines are all defined as 132 long.                    *
      //                                                                    *
      //        -  Note the fields CRLF, CR, & LF.                          *
      //              CR = Carriage Return                                  *
      //              LF = Line Feed                                        *
      //            This will be handy for you when you are constructing    *
      //            real records, later.                                    *
      //                                                                    *
      //                                                                    *
      //*********************************************************************
     H COPYRIGHT('(C) Copyright Booth Martin, 2012, All rights reserved.')
     H option(*nodebugio) dftactgrp(*no) actgrp(*new)
      //--------------------------------------------------------------------*
      // FILES                                                              *
      //--------------------------------------------------------------------*

      //--------------------------------------------------------------------*
      // DEFINITIONS                                                        *
      //--------------------------------------------------------------------*
      // Constants
      // Path and name for IFS Output.
     D cDirPath        c                   '/www/bmartin/testdir/'
     D cFileName       c                   'test.txt'
      //*********************************************************************
      //  Flags for use in open()
      //
      // More than one can be used -- add them together.
      //*********************************************************************
      //                                            Reading Only
     D O_RDONLY        c                   1
      //                                            Writing Only
     D O_WRONLY        c                   2
      //                                            Reading & Writing
     D O_RDWR          c                   4
      //                                            Create File if not exist
     D O_CREAT         c                   8
      //                                            Exclusively create
     D O_EXCL          c                   16
      //                                            Truncate File to 0 bytes
     D O_TRUNC         c                   64
      //                                            Append to File
     D O_APPEND        c                   256
      //                                            Convert text by code-page
     D O_CODEPAGE      c                   8388608
      //                                            Open in text-mode
     D O_TEXTDATA      c                   16777216

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

     D mkdir           PR            10I 0 ExtProc('mkdir')
     D   path                          *   Value options(*string)
     D   mode                        10U 0 Value

     D open            PR            10I 0 extproc('open')
     D   path                          *   value options(*string)
     D   oflag                       10I 0 value
     D   mode                        10U 0 value options(*nopass)
     D   codepage                    10U 0 value options(*nopass)

     D write           PR            10I 0 extproc('write')
     D   fildes                      10I 0 value
     D   buf                           *   value
     D   nbyte                       10U 0 value

     D/if not defined(CLOSE_PROTOTYPE)
     D close           PR            10I 0 extproc('close')
     D   fildes                      10I 0 value
     D/define CLOSE_PROTOTYPE
     D/endif

      // Working fields.
     D wNdx            s             10i 0
     D wMsg            s            132
     D wDirPath        s            132    varying
     D wFileName       s            132    varying

     D cASCII          c                   819
     D   CRLF          c                   x'0D0A'
     D   CR            c                   x'0D'
     D   LF            c                   x'0A'
     D   OpenFlags     s             10i 0
     D   FileModes     s             10u 0
     D wFileDesc       s             10i 0
     D wFD             s             10i 0

      /FREE
       //====================================================================*
       // MAINLINE-BEGIN                                                     *
       //====================================================================*
       wDirPath = cDirPath;
       wFileName = cFileName;

       // Create a directory, if it is not there:
        wFD = mkdir(wDirPath:
              S_IRUSR+S_IWUSR+S_IXUSR+
              S_IRGRP+S_IWGRP+S_IXGRP+
              S_IROTH+S_IXGRP);

        // Create a file, add records to a file, or clear file and then add records.
        //  OpenFlags = O_RDWR + O_CREAT + O_APPEND;  // adds records to file
            OpenFlags = O_RDWR + O_CREAT + O_TRUNC;   // clears file first.
       FileModes = S_IRUSR + S_IWUSR + S_IRGRP;
       wFD = Open
           (%trim(wDirPath + wFileName): OpenFlags: FileModes);
       // If an error:
       if wFD < 0;
         dsply '' ' ' wFD;
         exsr ExitPgm;
       endif;
       // Write records to the file. (Replace or Append, as chosen above.)
       exsr WriteFile;
       // Close the IFS File
       callp Close(wFD);
       // Exit.
       exsr ExitPgm;
       //====================================================================*
       // MAINLINE-END                                                       *
       //====================================================================*
       //-------------------------------*  Sub-Routine  *
       // ExitPgm()                     *---------------*
       // Exit program.                                 *
       //-----------------------------------------------*
       begsr ExitPgm;
         *inlr = *on;
         return;
       endsr;
       //-------------------------------*  Sub-Routine  *
       // WriteFile()                   *---------------*
       // Populate the file:                            *
       //-----------------------------------------------*
       begsr WriteFile;
       for wNdx = 1 to 50;  // For test only; rewrite for your needs.
         exsr WriteLine;
       endfor;              // (Writes 50 Test records.)
       endsr;
       //-------------------------------*  Sub-Routine  *
       // WriteLine()                   *---------------*
       // Write records to the file.                    *
       //-----------------------------------------------*
       begsr WriteLine;
         wMsg =            // For test only; rewrite for your needs.
               '"' + ' Test1-' + %trim(%editc(wNdx: 'Z')) + ' ",'
             + '"' + ' Test2-' + %trim(%editc(wNdx: 'Z')) + '",'
             + '"' + ' Test3-' + %trim(%editc(wNdx: 'Z')) + '",'
             + '"' + ' Test4-' + %trim(%editc(wNdx: 'Z')) + '",'
             + '"' + ' Test5-' + %trim(%editc(wNdx: 'Z')) + '",'
             + '"' + ' Test6-' + %trim(%editc(wNdx: 'Z')) + '",'
             + CR;
         // Write a record.
         callp Write(wFD: %addr(wMsg):
             %len(%trim(wMsg)));
       endsr; 
[Home] [Code Samples] [SQL (simple)] [Subfiles] [Web & .json] [Contact Us] [Other]