FOPEN(

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Scott Klement
Site Admin
Posts: 636
Joined: Sun Jul 04, 2021 5:12 am

Re: FOPEN(

Post by Scott Klement »

DATA-INTO is able to read the file from the IFS for you... For example:

Code: Select all

data-into putapiwms_err 
          %DATA(%TRIM(IFSFILE)
               :'DOC=FILE case=convert allowmissing=yes +
                 allowextra=yes trim=none countprefix=num_')
          %PARSER('YAJLINTO'); 
When DOC=FILE is given, the first parameter to %DATA is the IFS filename.

If the open API is returning -1, it means that something is wrong. You can retrieve the error number to determine what the error is...

Code: Select all

       dcl-s ptr_unix_error pointer;
       dcl-s unix_error int(10) based(ptr_unix_error);
       dcl-pr get_unix_error extproc('__errno') pointer;
       end-pr;
       
       .
       .

        FD = OPEN( %TRIM(IFSFILE)
                        : O_RDONLY + O_TEXTDATA + O_CCSID: 0: 1208);

        If (Fd < 0);
          p_errno = get_unix_error();
          wwErrorMsg = 'Open failed with CPE' + %char(unix_error);
          // TODO: send error message somewhere...
          FClose(Fd);
        Else; 
 
The parameters you are currently sending to open() don't make any sense. You are specifying the options used by O_TEXT_CREAT, but are not providing that parameter. You are specifying options used by O_CREAT, but have told us that the file should already be there, so why are you specifying that you wish to create it? If you don't specify O_CREAT, then there is no reason to specify the parameters for it.

Assuming you want it to translate whatever the IFS file happens to be to UTF-8 (1208), the flags I'm giving above should be correct.

If you don't want it to perform any translation at all (which would make sense since both your program and file are listed as 1208 in the example where you were creating the file) then you only need this:

Code: Select all

        FD = OPEN( %TRIM(IFSFILE): O_RDONLY);
But you may not need to use open() at all if you are willing to let data-into read the file for you.
jonboy49
Posts: 200
Joined: Wed Jul 28, 2021 8:18 pm

Re: FOPEN(

Post by jonboy49 »

Using your DATA-INTO as a base it would be this:

Code: Select all


dcl-s  IFS_filename  varchar(200)  inz('fullepathname_Of_IFSFile');

data-into putapiwms_err  %DATA( IFS_filename
                              : 'doc=file case=convert allowmissing=yes allowextra=yes trim=none countprefix=num_') 
                         %PARSER('YAJLINTO'); 
By the way, the -1 return probably indicates that the file can't be found - so you filename is wrong. I haven't seen how your filename was defined to the open() or indeed what your open() prototype looks like but for open() the filename must be trimmed of all RHS blanks _and_ be null (X'00') terminated. If the proto has Options(*String) specified then normally the name would be passed as %TrimR(filename). If the proto does not have that option (it should) then you would pass ( %TrimR(filename) + X'00' ) as the filename. If the filename is stored in a varchar field and was trimmed on loading then of course no %trim is needed.
IBMOLIVIER
Posts: 6
Joined: Fri Dec 02, 2022 2:34 pm

Re: FOPEN(

Post by IBMOLIVIER »

I used this solution and it works great

data-into putapiwms_err
%DATA(%TRIM(IFSFILE)
:'DOC=FILE case=convert allowmissing=yes +
allowextra=yes trim=none countprefix=num_')
%PARSER('YAJLINTO');

Thank you for your help
it's very much appreciated
Post Reply