IFS Write fails when using codepage 1208

Discussions relating to writing software in ILE RPG (RPG IV). This includes both fixed and free format RPG.
Post Reply
msddcb
Posts: 34
Joined: Wed Jul 28, 2021 5:12 am

IFS Write fails when using codepage 1208

Post by msddcb »

I have a requirement to create a file in the IFS with ccsid 1208 and the data in 1208.
The reason is I am using a php process file_get_contents that requires the data in 1208.
The following test program should compile easily.
When it runs if fails on the write with the error CPF9897 open(): Conversion error.
If I change the ccsid to 819 it runs fine.
Is it possible to create a file in the IFS with the date in codepage 1208 ?

Code: Select all

H  DatFmt(*ISO)                                                                  
H  Option(*SrcStmt)                                                              
H  ExprOpts(*ResDecPos)                                                          
H  FltDiv(*Yes)                                                                  
H  DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE') BNDDIR('IFSTEXT')                 
 * -----------------------------------------------------------------*            
 * -----------------------------------------------------------------*            
                                                                                 
 *****************************************************************  *            
 * DEFINE FILES TO USE                                              *            
 *****************************************************************  *            
                                                                                 
D fd              S             10I 0                                            
D line            S            100A                                              
D len             S             10I 0                                            
D msg             S             52A                                              
D err             S             10I 0                                            
                                                                                 
D/SPACE 3                                                                        
 // =====================================================================//      
 // Program status Data Structure                                                
 // SDSPGM - Program name                                                        
 // SDSPSD - Program status code when error occurred                             
  // SDSRTN - RPG Routine where error occurred                                  
 // SDSWS  - Workstation ID                                                    
 // SDSUSR - User Profile                                                      
 //=====================================================================//     
D SDS            SDS           429                                             
D  SDSPGM           *PROC                                                      
D  SDSPSD           *STATUS                                                    
D  SDSRTN           *ROUTINE                                                   
D  SDSPRM           *PARMS                                                     
D  SDSWS                244    253                                             
D  SDSUSR               254    263                                             
                                                                               
 /include IFSEBOOK/QRPGLESRC,IFSIO_H                                           
 /include IFSEBOOK/QRPGLESRC,ERRNO_H                                           
 /include IFSEBOOK/QRPGLESRC,IFSTEXT_H                                         
                                                                               
c                   eval      *inlr = *on                                      
C* Make sure we don't have an old file that might be in the way                
C* (ENOENT means it didnt exist to begin with)                                 
c                   if        unlink('/home/test1202.txt') < 0                 
c                   eval      err = errno                                      
c                   if        err <> ENOENT                                    
c                   callp     die('unlink(): ' + %str(strerror(err)))          
c                   endif                                                         
c                   endif                                                         
C* Create a new file, and assign it a code page of 1208:                          
c                   eval      fd = open('/home/test1208.txt':                     
c                                  O_CREAT+O_WRONLY+O_CODEPAGE:                   
c                                  S_IWUSR+S_IRUSR+S_IRGRP+S_IROTH:               
c                                  1208)                                          
c                   if        fd < 0                                              
c                   callp     die('open(): ' + %str(strerror(errno)))             
c                   endif                                                         
c                   callp     close(fd)                                           
C* Now re-open the file in text mode.  Since it was assigned a                    
C* code page of 819, and we're opening it in text mode, OS/400                    
C* will automatically translate to/from ASCII for us.                             
c                   eval      fd = open('/home/test1208.txt':                     
c                                  O_WRONLY+O_TEXTDATA        )                   
c                   if        fd < 0                                              
c                   callp     die('open(): ' + %str(strerror(errno)))             
c                   endif                                                         
                                                                                  
c                   eval      line = '<html>  </hml>'                             
c                   eval      len = %len(%trimr(line))                            
c                   callp     writeline(fd: %addr(line): len)                     
c                   callp     close(fd)                             
C*------------------------                                          
                                                                    
 /DEFINE ERRNO_LOAD_PROCEDURE                                       
 /COPY IFSEBOOK/QRPGLESRC,ERRNO_H                                   
Thanks
Don
peder udesen
Posts: 35
Joined: Thu Jul 29, 2021 8:00 am

Re: IFS Write fails when using codepage 1208

Post by peder udesen »

In the code you unlink the file test1202.txt
then you create a file test1208.txt

Could that be the reason to the error?
jonboy49
Posts: 262
Joined: Wed Jul 28, 2021 8:18 pm

Re: IFS Write fails when using codepage 1208

Post by jonboy49 »

You're using a very outdated method for checking/creating/openining the file. Had you used the single step approach the code would be much simpler and this naming issue could not occur.

You can open, clear (truncate) an existing file, set code page, and translate all in a single open. Here's the base example.

Code: Select all

// Set flags: Create, Write Only, Truncate existing, use CCSID, and translate text data
flags = O_CREAT + O_WRONLY + O_TRUNC + O_CCSID + O_TEXTDATA;

// Open file with CCSID 1208 (UTF-8). 
fd = open('/home/user/example.txt': flags: S_IRWXU: 1208: 0);
msddcb
Posts: 34
Joined: Wed Jul 28, 2021 5:12 am

Re: IFS Write fails when using codepage 1208

Post by msddcb »

Thanks Jon,
I checked my version of IFSIO_H in IFSEBOOK and it did not have the 5th parameter. I have now updated after downloading chksrv.zip from Skott's site.

(I actually do not recall where I originally got the library from but it must have been a long time ago??)

I have changed the program

Code: Select all

c                   eval      flags = O_CREAT + O_WRONLY + O_TRUNC    
c                              + O_CCSID + O_TEXTDATA        
c                   eval      fd = open('/home/test1208.txt': flags : 
c                                  S_IRWXU : 1208 : 0)                
                                                                      
c                   eval      line = '<html>  </hml>'                 
c                   eval      len = %len(%trimr(line))                
c                   callp     writeline(fd: %addr(line): len)         
c                   callp     close(fd)                          
Now when I run the program I do not get any errors but attempting to view the file in the ifs with wrklnk and option 5 gives error CPF9897
Message . . . . : 6 INVALID CHARCTERS FOR CCSID 01208 ENCOUNTERED IN DISPLAY
RECORD 2. IF THE RECORD IS CHANGED, THEY WILL BE REPLACED WITH BLANKS.
Cause . . . . . : No additional online help information is available.

This error is suggesting the data is not in 1208 ?

Or am I missing something else ?

Thanks
Don
jonboy49
Posts: 262
Joined: Wed Jul 28, 2021 8:18 pm

Re: IFS Write fails when using codepage 1208

Post by jonboy49 »

You forgot to use O_TEXT_CREAT on the open ... without it no conversion takes place. This code works as you want it to. It is free-form - I cannot force myself to work in fixed form any more.

Code: Select all

flags = O_CREAT + O_WRONLY + O_TRUNC + O_CCSID + O_TEXTDATA + O_TEXT_CREAT;
fd = open('/home/paris/test1208.txt': flags : S_IRWXU : 1208 : 0);
                                                                      
line = '<html>  </hml>' + CRLF;
len = %len(%trimr(line));
reply = write(fd: %addr(line): len);
reply = close(fd);
P.S. If you defined the "line" field as varying length you could avoid the %Trim and speed up the program. e.g.

Code: Select all

line = '<html>  </hml>' + CRLF;
reply = write(fd: %addr(line:*Data): %len(line);
msddcb
Posts: 34
Joined: Wed Jul 28, 2021 5:12 am

Re: IFS Write fails when using codepage 1208

Post by msddcb »

Thank you Jon, yes that worked.

Sorry about the fixed form code, it was a test program and I copied from one of Scott's tutorials. When applying to the application it will be free format.

Also I copied your earlier response to set variable flags which did not have O_TEXT_CREAT ;)

Do you have any knowledge of the library IFSEBOOK and where it came from or if there is a later version ?

Now that it works I will go back to the documentation and reread the descriptions for all the flags that can be set.

Thank you
Don
jonboy49
Posts: 262
Joined: Wed Jul 28, 2021 8:18 pm

Re: IFS Write fails when using codepage 1208

Post by jonboy49 »

Sorry - I didn't notice the missing O_TEXT_CREAT until I tried to get your code working. The code I posted was just a condensed version of your original. I never attempted to run it.

The only IFS E Book that I am aware of is Scott's - here: https://www.scottklement.com/rpg/ifs.html

As far as I know Scott has not updated it. His presentation on coding for the IFS https://www.scottklement.com/presentati ... %20IFS.pdf does cover the use of O_TEXT_CREAT.

Scott wrote articles on the additional IFS features over the years but sadly with the demise of Penton all those "disappeared" He has republished a few pieces on this website but not the IFS ones to my knowledge.
jonboy49
Posts: 262
Joined: Wed Jul 28, 2021 8:18 pm

Re: IFS Write fails when using codepage 1208

Post by jonboy49 »

Sorry - I didn't notice the missing O_TEXT_CREAT until I tried to get your code working. The code I posted was just a condensed version of your original. I never attempted to run it.

The only IFS E Book that I am aware of is Scott's - here: https://www.scottklement.com/rpg/ifs.html

As far as I know Scott has not updated it. His presentation on coding for the IFS https://www.scottklement.com/presentati ... %20IFS.pdf does cover the use of O_TEXT_CREAT.

Scott wrote articles on the additional IFS features over the years but sadly with the demise of Penton all those "disappeared" He has republished a few pieces on this website but not the IFS ones to my knowledge.
Scott Klement
Site Admin
Posts: 983
Joined: Sun Jul 04, 2021 5:12 am

Re: IFS Write fails when using codepage 1208

Post by Scott Klement »

The IFS eBook was written for V3R2. I haven't removed it from my site because people still use it, but it is VERY old.

The extra parameter to open was added in V5R2, much later than V3R2.

A newer version of the copybook can be found here:
https://www.scottklement.com/rpg/copybooks
Post Reply