Since you talk about C++, I think you should use C++ more effectively (see my comment to your question):
E.g.
Main function:
int Main()
{
CReadWriteCsv in1("In1.csv");
in1.read(1000);
CReadWriteCsv in2("In2.csv");
in2.read(1000);
in1.append("output.csv");
in2.append("output.csv");
return 0;
}
And the class to process CSV files:
#ifndef CREARWRITECSV_H
#define CREARWRITECSV_H
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
class CReadWriteCsv
{
public:
CReadWriteCsv(char* infile): m_infile(infile) {}
void read(int maxRecords)
{
ifstream is(m_infile);
read(is, maxRecords);
}
void append(char* outfile)
{
ofstream os(outfile, ios_base::app);
write(os);
}
private:
typedef vector<string> CRecord;
typedef vector<CRecord> CContent;
string m_infile;
CContent m_content;
void read(istream& is, int maxRecords)
{
m_content.clear();
string line;
while(maxRecords-- > 0 && getline(is, line))
{
CRecord record;
m_content.push_back(readLine(line, record));
}
}
void write(ostream& os)
{
for (CContent::iterator it=m_content.begin(); it!=m_content.end(); ++it)
{
writeRecord(*it, os);
}
}
CRecord& readLine(const string& line, CRecord& record)
{
string field;
istringstream iss(line, istringstream::in);
while(getline(iss, field, ',')) record.push_back(field);
return record;
}
void writeRecord(CRecord& record, ostream& os)
{
for(CRecord::iterator it=record.begin(); it!=record.end(); ++it)
{
if (it!=record.begin()) os << ",";
os << *it;
}
os << "\n";
}
};
#endif