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:

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
2022 License Price Increase and Consulting Update for Non-Software Specific Assistance 2022 License Price Increase and Consulting Update for Non-Software Specific Assistance
Posted by August 9, 2021
BVSTools >> BVSTools Announcements
Journaling the IFS, SPLTOOL and Java Journaling the IFS, SPLTOOL and Java
Posted by August 4, 2021
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
G4MSMAIL Now Allows Wildcard Attachments G4MSMAIL Now Allows Wildcard Attachments
Posted by June 10, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
G4G Update for Uploading Large Files G4G Update for Uploading Large Files
Posted by May 28, 2021
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Can We Purchase License Keys for More Than One Year at a Time? Can We Purchase License Keys for More Than One Year at a Time?
Posted by May 21, 2021
BVSTools >> BVSTools Announcements
JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages
Posted by May 10, 2021
BVSTools >> BVSTools Announcements >> Job Watch (JOBWATCH) Specific Announcements
Iseries Access data transfer Iseries Access data transfer
Posted by April 29, 2021
IBM Power Systems >> (QGPL) IBM i
YAJL - Parsing YAJL - Parsing
Posted by April 4, 2021
Programming >> RPG Programming
Create LPAR partition and install OS Create LPAR partition and install OS
Posted by March 24, 2021
IBM Power Systems >> (QGPL) IBM i
G4MS Now Allows User/Password Authentication Option G4MS Now Allows User/Password Authentication Option
Posted by March 16, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
IPDS printer file using CHRSIZ IPDS printer file using CHRSIZ
Posted by February 25, 2021
Programming >> RPG 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).