bvstone

Creating Stream Files with RPG and Mixed CCSID Data

Posted:

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



Latest Posts:

Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i
BVSTools Releases Braintree Webhook Open Source Application - Node.js BVSTools Releases Braintree Webhook Open Source Application - Node.js
Posted by April 5, 2018
Programming >> Open Source
BVSTools Now Offering Web Services (BETA) BVSTools Now Offering Web Services (BETA)
Posted by April 3, 2018
BVSTools >> BVSTools Announcements
Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2 Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2
Posted by March 29, 2018
Programming >> Web Programming
Still on V7R1 or Earlier?  Here's Why You Should Upgrade NOW! Still on V7R1 or Earlier? Here's Why You Should Upgrade NOW!
Posted by February 21, 2018
IBM Power Systems >> (QGPL) IBM i
Converting a MMDDYY date format to YYMMDD for Sorting Using SQL Converting a MMDDYY date format to YYMMDD for Sorting Using SQL
Posted by February 16, 2018
Programming >> RPG Programming
Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G) Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G)
Posted by February 3, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
Removing Unwanted Characters from a String with RPG Removing Unwanted Characters from a String with RPG
Posted by February 2, 2018
Programming >> RPG Programming
GETURI Version 8.10 Released GETURI Version 8.10 Released
Posted by January 23, 2018
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools is Now a Sponsor of ALLAS400S.com BVSTools is Now a Sponsor of ALLAS400S.com
Posted by January 17, 2018
BVSTools >> BVSTools Announcements
Service Pack SP171219 Released for G4PP v3.10 Service Pack SP171219 Released for G4PP v3.10
Posted by December 20, 2017
BVSTools >> BVSTools Announcements >> GreenTools for PayPal (G4PP) Specific Announcements
GETURI Now Requires Minimum OS of V7R1 or Higher GETURI Now Requires Minimum OS of V7R1 or Higher
Posted by December 12, 2017
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
Need Help Updating Your Software Versions?  We Can Help. Need Help Updating Your Software Versions? We Can Help.
Posted by December 8, 2017
BVSTools >> BVSTools Announcements
Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction
Posted by November 27, 2017
BVSTools >> BVSTools Software Discussion >> GreenTools for PayPal (G4PP) Specific Discussion

Reply




Copyright 1983-2018 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).