c ++跳过csv文件的第一行 [英] c++ Skip first line of csv file

查看:652
本文介绍了c ++跳过csv文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让我的程序从.csv文件读取并输出数据,但我不希望它输出第一行.我尝试使用getline(data, line);stream.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );.虽然确实跳过了第一行,但最后两行却打印两次并混合在一起.

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?

推荐答案

while (data.good())是可疑的.您最终又吃了"一行.参见例如为什么在循环条件内的iostream :: eof被认为是错误的?以获取更多详细信息.通常,您必须直接在while中测试getline的结果,例如

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)){...}

一种可能的解决方案是用while(getline(data, line)){...}逐行读取文件,然后使用stringstream(line),对于每一行,再次使用getline对其进行解析,现在用,分隔.要跳过第一行,只需先执行getline(data, line);,然后再执行while(getdata(data, line)){ /* process 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;
    }
}

这篇关于c ++跳过csv文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆