Search code examples
c++csvgetline

c++ Skip first line of csv file


I got my program to read from a .csv file and output the data but I don't want it to output the first line. I've tried to use getline(data, line); and stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );. While it does skip the first line, the last two lines print twice and are mixed up.

string ID;
string sentenceIn;
string servedIn;
int sentence;
int served;
string lastName;
string firstName;

vector<string> idNum;
vector<string> sentenceLen;
vector<string> servedTime;
vector<string> lastNameIn;
vector<string> firstNameIn;


ifstream data("prisoner_data.csv");

if (data.is_open())
{
    cout << "File opened successfully." << endl << endl;
    while (data.good()) // !someStream.eof()
    {
        getline(data, ID, ',');
        cout << ID << "  ";
        idNum.push_back(ID);

        getline(data, sentenceIn, ',');
        cout << sentenceIn << "  ";
        sentenceLen.push_back(sentenceIn);
        istringstream(sentenceIn) >> sentence;

        getline(data, servedIn, ',');
        cout << servedIn << "  ";
        servedTime.push_back(servedIn);
        istringstream(servedIn) >> served;

        getline(data, lastName, ',');
        lastNameIn.push_back(lastName);
        cout << lastName << "  ";

        getline(data, firstName, ',');
        firstNameIn.push_back(firstName);
        cout << firstName << "  ";
    }
}

What can I do to skip the first line without messing up the last?


Solution

  • The while (data.good()) is fishy. You end up "eating" one more line. See e.g. Why is iostream::eof inside a loop condition considered wrong? for more details. You usually have to test the result of getline directly in the while, like

    while(getline(data, line)){...}
    

    One possible solution is to read the file line by line with while(getline(data, line)){...} then use a stringstream(line) and for each line, parse it with getline again, now separated by ,. To skip the first line just do a getline(data, line); before, then follow up with while(getdata(data, line)){ /* process line */}. A simple example below:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    
    int main()
    {     
        std::ifstream data("prisoner_data.csv");
        if (!data.is_open())
        {
            std::exit(EXIT_FAILURE);
        }
        std::string str;
        std::getline(data, str); // skip the first line
        while (std::getline(data, str))
        {
            std::istringstream iss(str);
            std::string token;
            while (std::getline(iss, token, ','))
            {   
                // process each token
                std::cout << token << " ";
            }
            std::cout << std::endl;
        }
    }