简体   繁体   中英

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
                values.push_back(it->c_str());
            }

            // store array of values
            csv_values.push_back(values);
        }
    }
    else
    {
        cerr << "Error: Unable to open file " << argv[1] << endl;
        return -1;
    }

    // display results
    cout.precision(1);
    cout.setf(ios::fixed,ios::floatfield);

    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;
    }
}

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM