Forums >> Programming >> RPG Programming >>
Creating Stream Files with RPG and Mixed CCSID Data



Posted:
bvstone

Creating Stream Files with RPG and Mixed CCSID Data

 
Creating Stream Files with RPG and Mixed CCSID Data

I was recently working on a project with a customer where we were reading data from an input file in the IFS and writing that to another file.  We also needed to append data to it at the end.  This brought about the issue of the data in the file being a CCSID of 1252 (or 1208) but the literal text we were adding to it was of course in our job's CCSID (37).

This means we had a couple options:

Option 1: Read the data in from the input file as binary and write it out to the new file, again as binary data.  Then for our literal text, we need to use iConv() to convert our literal text from our job's CCSID to the CCSID of the file we are writing to.

Option 2: Read the data in from the input file as binary and write it out to the new file.  We would then close the new file, reopen it with the O_TEXTDATA flag and write our literal data to it and let the system do the conversion between our job's CCSID and the file's CCSID

What I came up with as a sample is the following program:

 

 

 

     H DFTACTGRP(*NO) BNDDIR('BVSTOOLS')
      ****************************************************************
      * Imports
      ****************************************************************
      /COPY QCOPYSRC,P.IFS
      /COPY QCOPYSRC,P.IFSSTD
      ****************************************************************
      * Global Definistions
      ****************************************************************
     D file1           S            256    INZ('/tmp/testccsid.txt')
     D file2           S            256    INZ('/tmp/testccsid_new.txt')
     D fd1             S             10i 0
     D fd2             S             10i 0
     D dataLen         S             10i 0
     D rc              S             10i 0
     D cp              S             10i 0
     D data            S          65535
      ****************************************************************
      /free

       mode = S_IRWXU + S_IRWXG + S_IRWXO;

       // open an input file in binary mode and read the data.
       oflag = O_RDONLY;
       fd1 = openstmf(%trimr(file1):oflag:mode);
       dataLen = #readStmf(fd1:%addr(data):%size(data));
       #closeStmf(fd1);

       // open the new file in binary mode.  Retrienve the CCSID of the file we opened
       //  previously and use that when we open the file to write more data to it.
       oflag = O_CREATE + O_TRUNC + O_WRONLY + O_CCSID;
       cp = #getStmfCCSID(file1);
       fd2 = openstmf(%trimr(file2):oflag:mode:cp);
       rc = #writeStmf(fd2:%addr(data):dataLen);
       #closeStmf(fd2);

       // open the original file in text mode again to write more data to it.
       oflag = O_APPEND + O_WRONLY + O_TEXTDATA + O_CCSID;
       fd2 = openstmf(%trimr(file2):oflag:mode:0);
       data = 'This is a test.';
       rc = #writeStmf(fd2:%addr(data):%len(%trimr(data)));
       #closeStmf(fd2);

       *INLR = *ON;

Now, of course we're missing a lot of error checking, but this is simply to show how the problem of mixing text data and binary data from other file (which could be text, or include single by character set or double byte character set data).

Next I wanted to see if I could write text data to a file, then read an existing stream file and write that to the file, then some more text data.  The following does just that:

     H DFTACTGRP(*NO) BNDDIR('BVSTOOLS')
      ****************************************************************
      * Imports
      ****************************************************************
      /COPY QCOPYSRC,P.IFS
      /COPY QCOPYSRC,P.IFSSTD
      ****************************************************************
      * Global Definistions
      ****************************************************************
     D file1           S            256    INZ('/tmp/testccsid.txt')
     D file2           S            256    INZ('/tmp/testccsid_new.txt')
     D fd1             S             10i 0
     D fd2             S             10i 0
     D dataLen         S             10i 0
     D rc              S             10i 0
     D cp              S             10i 0
     D data            S          65535
      ****************************************************************
      /free

       mode = S_IRWXU + S_IRWXG + S_IRWXO;

       // open and create the new file with a CCSID of 1208.  The 5th
       //  parm is 0 which means the data we write will be in the CCSID
       //  of our job and converted from that CCSID to 1208.
       oflag = O_CREATE + O_WRONLY + O_TRUNC + O_CCSID +
                 O_TEXTDATA + O_TEXT_CREAT;
       fd2 = openstmf(%trimr(file2):oflag:mode:1208:0);
       data = 'This is at the beginning and a test.' + X'0D25';
       rc = #writeStmf(fd2:%addr(data):%len(%trimr(data)));
       #closeStmf(fd2);

       // open an existing input file in binary mode.
       oflag = O_RDONLY;
       fd1 = openstmf(%trimr(file1):oflag:mode);
       dataLen = #readStmf(fd1:%addr(data):%size(data));
       #closeStmf(fd1);

       // open the original file in binary mode.  Retrienve the CCSID of the input file
       //  and use that when we open the file to write more data to it.
       oflag = O_APPEND + O_WRONLY + O_CCSID;
       cp = #getStmfCCSID(file1);
       fd2 = openstmf(%trimr(file2):oflag:mode:cp);
       rc = #writeStmf(fd2:%addr(data):dataLen);
       #closeStmf(fd2);

       // open the original file in text mode again to write more data to it.
       oflag = O_APPEND + O_WRONLY + O_TEXTDATA + O_CCSID;
       fd2 = openstmf(%trimr(file2):oflag:mode:0);
       data = 'This is a test.';
       rc = #writeStmf(fd2:%addr(data):%len(%trimr(data)));
       #closeStmf(fd2);

       *INLR = *ON;

Mixing CCSID data can get confusing, but hopefully this will help others in the future.


Last edited 06/21/2016 at 11:01:59


Reply




Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).