Programming Languages on Power

 View Only

 Use of open() on IFS stream files

  • C/C++ and Fortran
Ryan Speight's profile image
Ryan Speight posted 02/07/25 02:10 PM

I have a program I want to write in C that seems pretty simple.  Open a stream file in the IBM i's IFS using open().  Print the contents line by line using read() and printf/fprintf.  Close the file using close().

The file I'm using contains one line of data, abcdefghij0123456789\r\n

A few hiccups though that I can't seem to get past.  

  1. When I use open(), the file descriptor seems to always return a value of 0.  
  2. When I used read(), the the letters are printed to the console screen, but they are mostly A's with different accents, and the numbers aren't visible at all.
  3. The file closes as expected.

Below is the output.  I'm not sure how to make heads or tails of this.  I'm not sure if the flags in open() are correct.  The file has a CCSID of 1252, which is windows latin-1.  I've used CPY to change the CCSID to 367 and 37 but for whatever reason, nothing is printing as expected.

-----IFSTEST START-----          
File opened successfully!        
File is /operations/fos/test2.txt
fd is 0                          
                                 
bytesRead: 21                    
buffer: /ÂÄÀÁÃÅÇѦ               
[i:0] [/:97]                     
[i:1] [Â:98]                     
[i:2] [Ä:99]                     
[i:3] [À:100]                    
[i:4] [Á:101]                    
[i:5] [Ã:102]                    
[i:6] [Å:103]

[i:7] [Ç:104]                       
[i:8] [Ñ:105]                       
[i:9] [¦:106]                       
[i:10] [ :48]                       
[i:11] [ :49]                       
[i:12] [ :50]                       
[i:13] [ :51]                       
[i:14] [ :52]                       
[i:15] [ :53]                       
[i:16] [ :54]                       
[i:17] [ :55]                       
[i:18] [ :56]                       
[i:19] [ :57]                       
:13]iage Return [i:20] [            
bytesRead: 1                        
buffer:                             
[i:0] [ :10]                        
Press ENTER to end terminal session.                    


#C/C++andFortran
Ryan Speight's profile image
Ryan Speight

I've changed the functions to fopen, fgets, and fclose.  I still get the strange characters.


#C/C++andFortran
Ryan Speight's profile image
Ryan Speight

It most obviously wasn't the functions used, but the CCSID and the data format.

I initially copied the file to a different directory using CPY and TOCCSID(*PCASCII) which did apply the CCSID as 1252.  Using read() or fgets() returned incorrectly presented data.

I then used CPY and TOCCSID(*CALC) DTAFMT(*TEXT) and using fgets() allows the characters to print to the console correctly.  

I'll have to dig in a bit more to figure out what I'm missunderstanding.


#C/C++andFortran
Robert Cozzi's profile image
Robert Cozzi

You have to open it with 2 CCSIDs. The one the file's data is in, and the other typically 0 for JOB CCSID.

Look at the (IIRC) the 4th and 5th or possibly the 5th and 6th parameters of open() to open it as a text file that is to be automatically translated. You'll also need the O_TEXTDATA flag.

Paul Nicolay's profile image
Paul Nicolay

There's no need to provide two CCSID's (the defaults are fine)... you need to specify the O_TEXTDATA on the flag parameter (ie. second parameter)

Anton Gombkötö's profile image
Anton Gombkötö

AFAIK, that is only true if your QCCSID system value isn’t 65535. (Which is generally a bad idea, but old installations seldom dare to change.)
With two CCSIDs, it works. (Except 1208, for which i had to use iconv.)
But these experiences are several years old, mabye it became better.
Just my 0.02 Euro, best regards
Anton Gombkötö

Paul Nicolay's profile image
Paul Nicolay

I don't think 65535 is an issue in this case as I never specify the job CCSID (neither on iconv API's) and never had an issue with it.

I guess they use the Default CCSID in that case.