简体   繁体   中英

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?

The while (data.good()) is fishy. You end up "eating" one more line. See eg 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;
    }
}

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