5.5. Using code pages with text files

So, now we've created some text files, but there's one small problem. They're all in EBCDIC! This means that although we can read them using OS/400, they're utterly useless on the PC.

Now, we could call an API like QDCXLATE or iconv() to convert the text to ASCII before we write it to disk, but then our OS/400 programs wouldn't be able to work with it!

This problem can be easily solved, because the IFS allows us to assign a code page to our stream file. There are many code pages used to support all of the various characters used all around the world.

For our examples, we will use code page 37, which represents EBCDIC in the United States where I live, and code page 819 which represents ASCII here. If you're interested in using other code pages, please see the National Language Support manual which is published by IBM, and included in your Softcopy Library or Information Center.

The O_CODEPAGE flag on the open() API is used to assign a code page to a text file when the file is created.

So, when we want to assign a code page, we do this:

     c                   callp     unlink('/ifstest/somefile.txt')

     c                   eval      fd = open('/ifstest/somefile.txt':     
     c                                  O_CREAT+O_WRONLY+O_CODEPAGE:      
     c                                  mode: 819)                              

Note that a new code page is assigned only if a new file is created. Therefore, if we really want to make sure that it's going to be assigned, we delete the file first.

The O_TEXTDATA flag on the open() API is used to tell the API that we're working with text data, so we want the system to translate the data for us. Note that it only does the translation if the file already exists, and is assigned a different code page than the one that's associated with our job.

To open the file so that the translation takes place, we'd open it like this:

     c                   eval      fd = open('/ifstest/somefile.txt':   
     c                                       O_RDWR+O_TEXTDATA)       

Now when we read or write any data to that file, it will automatically be translated to or from the code page that was assigned to it.