Datastage Parallel (C++) routine to create files dynamically

April 26, 2008 · Filed Under DataStage Articles, SOA, Tech Articles 

A Datastage parallel (C++) routine to create files dynamically for every record in single job. This is a feature most of the time a SOA or real time environment demands. From a stream of sinlge source or multiple sources create files for each input record or for a set of records according to a condition. With this routine you can dynamically pass your file path, file name and extension and also the records to be written into the file. If you want multiple records to be written into one file, store each record in a stage variable with new line character and finally pass that to this routine call. Records can be of different metadata.

pxCreateFile: Parallel routine (C++) which creates files dynamically.
—————————————————————————————————–
//Creates and writes on a text file for each record

//Pass the FileName with path as first parameter Ex: : LinkName.InputColumn : ‘.txt’

//Pass the Content of the file as second parameter Ex: LinkName.InputColumn

//Px routine call –> pxCreateFile( : LinkName.InputColumn : ‘.txt’, LinkName.InputColumn)

//If file creation is success 0 is returned, else 1. Use this status for report/tracking/print to file/print to peek

//If file already exists, overwrite happens

//Pass (empty) as second Parameter if you don’t want anything to be printed in the file)

#include < iostream>
#include < fstream>
using namespace std;

int pxCreateFile(char *FileName, char *Msg)

{
int StatusFlag=0;
ofstream myfile (FileName);
if (myfile.is_open())
{
myfile < < Msg;
myfile << "\n"; //Comment this line if a new line char is not required at the end of the file by default
myfile.close();
}
else
{
StatusFlag = 1;
}
return StatusFlag;
}

To contribute/enhance to this code/article you can find the wiki entry for the same in this link.

Comments

One Response to “Datastage Parallel (C++) routine to create files dynamically”

  1. Katacrava on June 26th, 2008 8:04 pm

    favorited this one, man

Leave a Reply