Hi,
I have some data in csv/txt file in below format:
7/11/2012 6:45,7/12/2012 6:45,1.0139,1.0123,1.0067,1.0236,1.0253,1.0289,
7/11/2012 6:45,7/12/2012 6:45,96.95,97.08,96.86,0,0,0,
7/11/2012 6:45,7/12/2012 6:45,1.5507,1.5487,1.5441,1.5623,1.5639,1.5691,
I am trying parsing this lines and write in another csv file. CSV must be include different rows & columns.
My code is:
void readInput(char* fileName) {
printf("****************ReadInput***************\n");
ifstream fin(fileName);
string tmp = ""; while(getline(fin, tmp)) {
listString.push_back(tmp);
cout << listString[listString.size() - 1] << endl;
}
printf("\n\n");
fin.close();
}
void parseValues() {
printf("****************ParseValue***************\n");
for (int i = 0; i < listString.size(); ++i) {
char tmp[100000];
strcpy(tmp, listString[i].c_str()); stringArray tmpArray;
char * pch;
pch = strtok (tmp,",");
while (pch != NULL) {
tmpArray.push_back(pch);
printf ("%s\t",pch);
pch = strtok (NULL, ",");
}
data.push_back(tmpArray);
printf("\n");
}
}
void writeOutput01(char* fileName) {
ofstream fout(fileName);
for (int i = 0; i < listString.size(); ++i) {
for (int j = 0; j < data[i].size(); ++j)
fout << data[i][j] << ',';
fout << "\n";
}
fout.close();
}
When it parsing and write, writes in 3 rows and one column.
It writes in one column instead 8 columns for each line.
How to fix this?
Regards,