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:

What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System? What Objects Should I Omit from Replication to Ensure My License Keys Work on my HA/DR System?
Posted by June 27, 2022
BVSTools >> BVSTools Software Discussion
GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page GreenTools for Google Apps (G4G) v15.00 Now Offers Functions to Bypass Registration Command and BVSTools Landing Page
Posted by May 3, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
How Do I Switch From MAILTOOL Plus to GreenTools for Google or Microsoft Office 365? How Do I Switch From MAILTOOL Plus to GreenTools for Google or Microsoft Office 365?
Posted by April 18, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
PTFs Issued for SSL/TLS Issues PTFs Issued for SSL/TLS Issues
Posted by March 12, 2022
IBM Power Systems >> PTF Watch
Google Dropping Support for Google Dropping Support for "Less Secure Apps" May 30th, 2022. What Does This Mean for Your IBM i Email?
Posted by March 4, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different?  Check the Command Defaults! Have You Installed a New Version of MAILTOOL and Now Things Are Acting Different? Check the Command Defaults!
Posted by February 28, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older Using MAILTOOL Plus on V7R1, or Any OS Using TLS 1.1 or Older
Posted by January 27, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names BVSTools ILE Functions Being Updated to Remove Hashtag (#) from Function Names
Posted by December 30, 2021
BVSTools >> BVSTools Announcements
GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names GETURI v12.00 Released Removing Beginning # (Hashtag) From Function Names
Posted by December 28, 2021
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
Is any of BVSTools Software Affected by the log4j exploit? Is any of BVSTools Software Affected by the log4j exploit?
Posted by December 20, 2021
BVSTools >> BVSTools Software Discussion
GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files GreenTools for Microsoft Apps (G4MS) Updated to Allow Downloads, Deletes, and Sharing of Files
Posted by December 17, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products SSL Handshake Errors with GETURI, MAILTOOL and GreenTools Products
Posted by October 18, 2021
BVSTools >> BVSTools Software Discussion
MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered MAILTOOL Updated to Retry Sending when GSK SSL Handshake Error 415 (GSK_ERROR_BAD_PEER) is Encountered
Posted by August 19, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers MAILTOOL Updated to Allow List-Unsubscribe and User Defined Headers
Posted by August 13, 2021
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
AWS signing process in as400 AWS signing process in as400
Posted by August 13, 2021
Programming >> Web Programming

Reply




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