blog icon indicating copy to clipboard operation
blog copied to clipboard

Creating and writing to spool files (intro)

Open worksofliam opened this issue 5 years ago • 0 comments

I found out recently that it's so simple to create and write to spool files. Why you would you want to write to spool files, you may ask. I want spool files to be written to as logs. For example, with Relic Package Manager, you have an option to print the log to the screen as a build happens - or instead to print it to a spool file (useful with SBMJOBs).

There really is only four lines that we need to declare, open, write and close. Oh, we also need to delcare a data structure since WRITE only accepts a data structure with single subfield of Char(132) in this instance - I have no idea why, but seems very odd.

  1. Dcl-F QsysPrt Printer(132) UsrOpn; is used to declare a printer.

  2. The following code:

Dcl-Ds gLine;
  *N Char(132);
End-Ds;

We need this DS as the Write opcode only accepts a DS. Notice that the length of the subfield is the same length as the Printer in declaration of QsysPrt.

  1. Open QsysPrt; is used to open the printer, which actually creates the spool file.

  2. Write QsysPrt gLine; writes a line to the spool file. Note that if gLine is *BLANK nothing will write.

  3. Close QsysPrt; closes the spool file, ending the ability to write.

Program example

Let's take this example program

Dcl-F QsysPrt Printer(132) UsrOpn;

Dcl-Ds gLine;
  *N Char(132);
End-Ds;

//******************

Open QsysPrt;

gLine = 'Spool file line 1';
Write QsysPrt gLine;

gLine = 'Spool file line 2';
Write QsysPrt gLine;

gLine = 'Spool file line 3';
Write QsysPrt gLine;

gLine = '';
Write QsysPrt gLine;

gLine = 'Spool file line 5';
Write QsysPrt gLine;

Close QsysPrt;

*InLR = *On;
Return;

After you have compiled this code (I called my program TEST1) you can call it (CALL TEST1), which should result on something on your user OUTQ - at least mine did. You should notice that the name of the spool file is what we named our declared file in the RPG program and that the User Data is the name of the program.

image

If you use 5 to display against the spool file, the output should be what you had written in your program.

image

That's it! I hope this helped you in developing some useful tool you're making.

worksofliam avatar Jul 07 '19 02:07 worksofliam