I am having a little problem file processing a csv file. I am very new to C++ and trying to learn. It's probably a little thing I am overlooking but I have searched for answers online and cannot figure out where I am going wrong. I am trying to process a file that has multiple lines and comma separated values (no comma at the end of the line though if that makes a difference) -- of note, when I tried to post the text just now it did not include the paragraph breaks, I had to add that in manually-- not sure if that makes a difference
Sale,11/9/14,11/9/14,AMAZON MKTPLACE PMTS,-8.99
Sale,10/4/14,10/5/14,AMAZON MKTPLACE PMTS,-13.08
Sale,10/3/14,10/5/14,AMAZON MKTPLACE PMTS,-9.82
Sale,10/2/14,10/3/14,AMAZON MKTPLACE PMTS,-45.48
Sale,8/21/14,8/22/14,AMAZON MKTPLACE PMTS,-9.99
Sale,11/8/14,11/9/14,Amazon.com,-64.7
Sale,10/1/14,10/2/14,APL* ITUNES.COM/BILL,-1.08
Sale,9/15/14,9/16/14,APL* ITUNES.COM/BILL,-1.08
I tried using getline
to get each line into a stringstream
then parse out each of those lines by the comma delimiter using the code below:
ifstream file("test1.csv");
string value, line;
while (getline(file, line)) {
stringstream linestream(line);
while (getline(linestream, value, ',')) {
cout << "Value: " << value << endl;
} // while
cout << "Done Procesing" << endl;
} // while
The problem I am getting is that for some weird reason after every 5th token of the comma delimited processing the word “Sale”
overwrites the word Value
and I cannot understand why. Would really appreciate some guidance.