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:

GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming

Reply




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