vlavyb vlavyb - 1 year ago 134
C++ Question

CSV parser in C++ doesn't read the first element

I extracted this piece of code to parse a CSV file, however it doesn't read the first element for the first n-1 rows. I can't figure it out why, and when I copy the data into a new empty file and save it as CSV file, the error disappears and it works fine. Here are the links for the original (error happens) and the copied (error doesn't happen) CSV file. Could you please help me as why this is happening?

Thank you.

#include <boost/tokenizer.hpp>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>

int main(int argc, char** argv)
using namespace std;

if (argc != 2)
cerr << "Usage: " << argv[0] << " <csv file>" << endl;
return -1;

vector< vector<string> > csv_values;

fstream file(argv[1], ios::in);

if (file)
typedef boost::tokenizer< boost::char_separator<char> > Tokenizer;
boost::char_separator<char> sep(",");
string line;

while (getline(file, line))
Tokenizer info(line, sep); // tokenize the line of data
vector<string> values;

for (Tokenizer::iterator it = info.begin(); it != info.end(); ++it)
// convert data into double value, and store

// store array of values
cerr << "Error: Unable to open file " << argv[1] << endl;
return -1;

// display results

for (vector< vector<string> >::const_iterator it = csv_values.begin(); it != csv_values.end(); ++it)

const vector<string>& values = *it;

for (vector<string>::const_iterator it2 = values.begin(); it2 != values.end(); ++it2)
cout << *it2 << " ";
cout << endl;

Answer Source

New lines in your orginal file ends with a carriage return, which is read by your code with last varible in the line, and then printed. So first line is printed like this

1 2 3 4 5\r

and then you print space, which is printed at the beginning of the line, covering "1".

You could easily see that in debugger :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download