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: 32
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: 33
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: 258
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);
Post Reply