Page 1 of 2

FOPEN(

Posted: Fri Dec 02, 2022 2:44 pm
by IBMOLIVIER
Hello I would need help
FD = FOPEN(%TRIM(IFSFILE)
: O_WRONLY + O_CREAT + O_TRUNC + O_TEXTDATA +
O_INHERITMODE + O_CCSID
: 0
: 1208
: 1208);
If (Fd < 0);
FClose(Fd);
Else;
Bytesread = FRead(Fd:%addr(Dataread):%size(DataRead));
Data = %Subst(DataRead:1:Bytesread);
Fclose(fd);
CdeRetour = *Blanks;
Endif;


I run this command, the contents of the file contain an accent car (French) ,
This explains the 1208 in CCSID
Here is the content of the IFS file before FOPEN(

{"statut":"Error","evenementId":"8dc9d47f-b7a1-4b7a-b615-5c685ac25130","validationResultats":[{"message":"L'evenement de stock ne permet pas de créer un lot car il ne contient pas d'Article","logLevel":"Error"}]}

Then after FOPEN(



Content disappears
Can you help me
Thank you

Re: FOPEN(

Posted: Sat Dec 03, 2022 12:46 am
by Scott Klement
This code makes no sense. You are writing the code as if you are calling the open() and read() APIs -- but your code says fopen and fread which are different APIs with different parameters.

Also, you posted this in the HTTPAPI forum, and it doesn't appear to be related at all to HTTPAPI.

Re: FOPEN(

Posted: Sat Dec 03, 2022 12:59 pm
by jonboy49
In addition Scott, unless I am misreading it, he is also opening the file for write with O_TRUNC which I believe is what is causing the file to be empty after the close.

Looks like a case of cloning an existing piece of IFS code.

Re: FOPEN(

Posted: Sun Dec 04, 2022 1:56 am
by Scott Klement
Well, if he were calling the open() API, yes. O_TRUNC makes no sense in the context of fopen()

Re: FOPEN(

Posted: Sun Dec 04, 2022 12:38 pm
by jonboy49
Scott Klement wrote: Sun Dec 04, 2022 1:56 am Well, if he were calling the open() API, yes. O_TRUNC makes no sense in the context of fopen()
Good point - I must try to stop answering questions before I'm awake!

But what could cause the file content to disappear then?

Re: FOPEN(

Posted: Mon Dec 05, 2022 2:53 am
by Scott Klement
Well, he's probably calling open(), but just to confuse us, has renamed it to fopen().... so the O_TRUNC is the likely culprit.

It'd be nice if he'd come on here and tell us that so we don't have to guess.

Re: FOPEN(

Posted: Mon Dec 05, 2022 9:30 am
by IBMOLIVIER
Hello, first of all my apologies for the confusion

Indeed, it is the Open and Read function
It's a program I copied

Thank you for your reply

I'm going to do a test

Re: FOPEN(

Posted: Tue Dec 06, 2022 9:05 am
by IBMOLIVIER
Hello, I can not recover the contents of this Json file

{"statut":"Error","evenementId":"4b0cfac1-377c-44e8-86f5-e6013725c465","validationResultats":[{"message":"L'evenement de stock ne permet pas de créer un lot car il ne contient pas d'Article","logLevel":"Error"}]}


The code :

dcl-ds putapiwms_err qualified;
statut varchar(10);
evenementId varchar(50);
dcl-ds validationResultats dim(1);
message varchar(200);
logLevel varchar(10);
end-ds validationResultats;
end-ds putapiwms_err;


Http_setCCSIDs( 1208: 0 );

Rc = http_req( 'POST': URL: Filename: *omit: *omit: PRM_DATA: 'application/json');
If Rc = 1;
Prm_Retour = '200' ;
unlink(Filename);
else;
Prm_Retour = %Char(Rc);

Extract json file info
Reponse = GetIfsFile(%Trim(%subst(Filename:1:FileNameLen)):
Data_reponse);

Insert the contents of the .json into a table

data-into putapiwms_err %DATA(Data_reponse
:'doc=string case=convert allowmissing=yes +
allowextra=yes trim=none countprefix=num_')
%PARSER('YAJLINTO');
Prm_MessageErreur = putapiwms_err.validationResultats(1).message;
Prm_LogLevel = putapiwms_err.validationResultats(1).logLevel;
endif;
on-error;
http_error(Erreur_http);
wwErrorMsg = http_error(Erreur_http);
http_dmsg('SetError() #' +
%trim(%editc(Erreur_http:'L')) +
': '+ wwErrorMsg);
Prm_Retour = %subst(wwErrorMsg:10:3);
endmon;

Return;

dcl-proc GetIfsFile;
dcl-pi *N ind;
IfsFile char(32767) const options(*varsize);
Data char(32768);
end-pi;
FD = OPEN(%TRIM(IFSFILE)
: O_WRONLY + O_CREAT + O_TEXTDATA +
O_INHERITMODE + O_CCSID
: 0
: 1208
: 1208);


If (Fd < 0);
FClose(Fd);
Else;
// Lecture fichier Json

Bytesread = Read(Fd:%addr(Dataread):%size(DataRead));
Data = %Subst(DataRead:1:Bytesread);
Fclose(fd);
CdeRetour = *Blanks;
UNLINK(%TRIM(IFSFILE));
Endif;
Error handling
On-Error;
CdeReturn = 'ERROR';
Dump(a);
Endmon;

If CdeRetour = *Blanks;
Return *ON;
Else;
Return *Off;
Endif;
End-proc GetIfsFile;


FD = -1 so I jump on the management of On-Error, I do not understand why
Could this be a story from CCSID 1208 , because as you have noticed the message is in French with an accent

Re: FOPEN(

Posted: Tue Dec 06, 2022 11:33 am
by jonboy49
I mentioned this before but you seem to have missed it.

You are specifying O_WRONLY on the open(). That says explicitly only to allow _write_ operations! So it is not surprising that a read() fails.

In fact, if you are opening for read your options really make no sense. Look them up and you'll see. In particular, O_CREAT makes no sense.

Why are you not just jetting DATA-INTO read the file for you?

Re: FOPEN(

Posted: Wed Dec 07, 2022 8:15 am
by IBMOLIVIER
Thanks jonboy for the advice
So I removed O_CREAT and O_WRONLY
I still have -1 when I come back from the IFSFILE OPEN
You were asking me this question ...
“Why are you not just jetting DATA-INTO read the file for you? “

I must use the OPEN function before making a DATA_INTO ?? right ??
If not, could you help me by seeing the code how to do it?


Thank you