1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
using namespace std;
void error(const char* p, const char* p2 = " ")
{
cerr << p << ' ' << p2 << '\n';
std::exit(1);
}
int main (int argc, char* argv[])
{
if (argc != 2) error("wrong number of arguments");
std::ifstream infile(argv[1]); // open input file stream
if (!infile) error("cannot open input file", argv[1]);
string line, firstline, outline, outfname, prevdate, currdate;
int linenum = 1;
int itemnum = 0;
/**** Need to get the first line which has the headers and save into a string to output to all files ****/
getline (infile, line);
istringstream linestream(line);
string item;
firstline = "";
while (getline (linestream, item, ','))
{
if (itemnum>0) firstline += " ";
firstline += item;
itemnum++;
}
firstline += '\n';
prevdate = " ";
while (getline (infile, line))
{
outline = "";
linenum++;
istringstream linestream(line);
itemnum = 0;
while (getline (linestream, item, ','))
{
if (itemnum>0) outline += " ";
outline += item;
itemnum++;
}
/* last item is the date in the file name format yyyymmdd */
/* need to check to see if outfile is open, then close when last row is reached */
/* so need some kind of flag to signal change in date */
currdate = item;
if (currdate!=prevdate)
{
//if (linenum!=2) outfile.close(); // NEED TO EXPLICITLY CLOSE THE PREVIOUS FILE??
outfname = "U235_" + currdate;
ofstream outfile ( (outfname).c_str() );
/*
std::ofstream outfile(outfname); // open output file stream
*/
if (!outfile) error ("cannot open output file", outfname);
outfile << firstline;
prevdate = currdate;
}
outline += '\n';
/*
outfile << outline;
*/
}
//outfile.close(); // NEED TO EXPLICITLY CLOSE THE PREVIOUS FILE??
return 0;
}
|