Troubleshooting the QC2LE write subproc

Discussions relating to writing software in ILE RPG (RPG IV). This includes both fixed and free format RPG.
Post Reply
imaxeman69
Posts: 23
Joined: Thu Mar 17, 2022 2:29 pm

Troubleshooting the QC2LE write subproc

Post by imaxeman69 »

I have a weird thing going on. I have the following code in one program:

Code: Select all

       fd = open('/mwalter/urlUSed.txt':
          O_WRONLY+O_CREAT+O_TRUNC+O_APPEND+O_CODEPAGE :
          S_IRGRP + S_IWGRP + S_IXGRP :
          819);
       callp close(fd);

       oFlag = O_WRONLY+O_APPEND+O_TEXTDATA;
      // Open the file and write the URL to the file.
       fd = open('/mwalter/urlUsed.txt':OFlag);
       if fd > 0;
         callp write(fd:%addr(theUrl):%len(%trimr(theUrl)));

       callp close(fd);
       endif;                                          
This code works fine. However, I have this code in another program:

Code: Select all

         inboundFile = '/P44/Inbound/' + %trim(title) + '_' +
                       %char(%date():*iso0) + '_' +
                       %char(%time():*hms0) + '.json';

          fd = open(inboundFile:
          O_WRONLY+O_CREAT+O_TRUNC+O_APPEND+O_CODEPAGE :
          S_IRGRP + S_IWGRP + S_IXGRP :
          819);
         callp close(fd);

         oFlag = O_WRONLY+O_APPEND+O_TEXTDATA;
        // Open the file and write the URL to the file.
         fd = open(inboundFile:OFlag);
         if fd > 0;
           jsonData = %trim(jsonData) + 'CRLF';
           myint = write(fd:%addr(jsonData):%len(%trim(jsonData)));
              // 1208);

           callp close(fd);
           EXEC SQL
             INSERT INTO Wserlgfl
             VALUES(CURRENT TIMESTAMP,
                    'write',
                    'Write Completed');
         else;
           EXEC SQL
             INSERT INTO Wserlgfl
             VALUES(CURRENT TIMESTAMP,
                    'write',
                    'Write failed. FD = ' || CHAR(myint));

         endif;                            
And the second Open in this program returns -1. Not sure why. The only difference is that the first program uses a set file name, and the second program uses a dynamic one.

Thanks,
imaxeman69
Posts: 23
Joined: Thu Mar 17, 2022 2:29 pm

Re: Troubleshooting the QC2LE write subproc

Post by imaxeman69 »

Ok, by eliminating the second open, I'm able to write to the stream file. However, the text is garbage. I'm guessing this will have something to do with the CODESET/CCSID.

Any ideas?
jonboy49
Posts: 97
Joined: Wed Jul 28, 2021 8:18 pm

Re: Troubleshooting the QC2LE write subproc

Post by jonboy49 »

No sure why you are doing two opens in the first place - the need to open to create and then open to write went away eons ago.

You appear to be creating with a windows (819) code page - but JSON should be 1208 shouldn't it?

Just one other thought - if you build your JSON in a variable length field you can avoid all the trims and messing about.
imaxeman69
Posts: 23
Joined: Thu Mar 17, 2022 2:29 pm

Re: Troubleshooting the QC2LE write subproc

Post by imaxeman69 »

I've tried 1208 and 819. Both give me garbage. That Bob Cozzi thing with opening and closing then opening again just seems to work, so that's what I do. I've actually found a more elegant solution. I save the JSON in a CLOB in a database table. THen, in Profound Logic, I wrote a program that displays the JSON. Feed this from a subfile grid and there aren't a bunch of IFS files hanging around.

THanks for your help.
jonboy49
Posts: 97
Joined: Wed Jul 28, 2021 8:18 pm

Re: Troubleshooting the QC2LE write subproc

Post by jonboy49 »

If I remember correctly the only reason for the two opens was that you could not specify both Create and Code page on the open. Once that restriction was removed then the double-open was no longer needed. That happened eons ago - maybe back in the late 1990s?

Anyway - glad you have a solution that works for you.
Scott Klement
Site Admin
Posts: 305
Joined: Sun Jul 04, 2021 5:12 am

Re: Troubleshooting the QC2LE write subproc

Post by Scott Klement »

imaxeman69 wrote: Wed Jul 06, 2022 4:30 pm I've tried 1208 and 819. Both give me garbage. That Bob Cozzi thing with opening and closing then opening again just seems to work, so that's what I do.
I question what this has to do with Bob Cozzi? This technique was needed prior to V5R2, when the new functionality was added.

It won't work with CCSID 1208 because you're using O_CODEPAGE (i.e. code pages) not O_CCSID. That's why you have to use 819 (which is ISO-8859-1, not related to Windows... it is an ISO standard for ASCII that actually predates Windows.) UTF-8 cannot be represented by a code page, it requires CCSID support, and that's probably what's causing your issue.

You also aren't trimming blanks off of "inboundFile" -- which may (or may not) be a problem.

Also you're passing the 'CRLF' string, so will write the letters C, R, L and F to the file instead of the carriage return and linefeed characters -- that really makes no sense at all.

Try coding it like this:

Code: Select all

         dcl-c CRLF x'0d25';

         inboundFile = '/P44/Inbound/' + %trim(title) + '_' +
                       %char(%date():*iso0) + '_' +
                       %char(%time():*hms0) + '.json';

          fd = open( %trimr(inboundFile)
                   : O_WRONLY+O_CREAT+O_TRUNC+O_APPEND+O_CCSID 
                     + O_TEXTDATA + O_TEXT_CREAT 
                   : S_IRGRP + S_IWGRP + S_IXGRP 
                   : 1208 
                   : 0 );

         if fd > 0;
           jsonData = %trim(jsonData) + CRLF;
           myint = write(fd:%addr(jsonData):%len(%trim(jsonData)));
              // 1208);

           callp close(fd);
    ... etc ...
imaxeman69 wrote: Wed Jul 06, 2022 4:30 pm I've actually found a more elegant solution. I save the JSON in a CLOB in a database table. Then, in Profound Logic, I wrote a program that displays the JSON. Feed this from a subfile grid and there aren't a bunch of IFS files hanging around.
I assume you mean "in Profound UI" (Profound Logic is the name of a company, not a software package). But, yeah, that works too.. the IFS solution would be much more efficient on disk space vs. saving it into a CLOB, though. Plus, it sounds like you are converting it to EBCDIC, which may be problematic if you need character codes that EBCDIC doesn't support. (Though, you could use a DBCLOB with CCSID 1200 to support Unicode with a LOB field... so the same approximate technique works fine.)
Scott Klement
Site Admin
Posts: 305
Joined: Sun Jul 04, 2021 5:12 am

Re: Troubleshooting the QC2LE write subproc

Post by Scott Klement »

I didn't test my example, above. If for some reason it doesn't work, please make sure you capture the value of ERRNO when it fails. (Printing the FD isn't very useful.)
https://www.scottklement.com/rpg/ifs_ebook/errors.html
Post Reply