简体   繁体   English

如何在C ++中跳过读取CSV文件的第一列

[英]How to skip the first column reading csv file in c++

Below code is to get some data from csv file. 下面的代码是从csv文件中获取一些数据。 But the result is showing as follow: 但是结果显示如下:

5,Jones,123-123-1234,BCBS,GP1234,39,Sarah,Broken Arm,3 5,琼斯,123-123-1234,BCBS,GP1234,39,莎拉,断臂,3

6,Smith,123-231-1234,UHC,G1234,47,Francine,Physical Therapy,03/25/2015 6,Smith,123-231-1234,UHC,G1234,47,Francine,Physical Therapy,03/25/2015

9,Adams,123-123-4321,Cigna,U1234,28,Bob,Broken Arm,2 9,亚当斯,123-123-4321,Cigna,U1234,28,鲍勃,断臂,2

5,Van Gogh,123-321-1234,BCBS,GP1235,37,Andrea,Tummy Ache,3 5,梵高,123-321-1234,BCBS,GP1235,37,安德里亚,肚子疼,3

10,Pewterschmidt,123-312-1234,UHC,G1112,42,Peter,Supervision normal first pregnancy,03/26/2015 10,Pewterschmidt,123-312-1234,UHC,G1112,42,Peter,监督正常第一次怀孕,2015年3月26日

But I want to get the data except first column(such as 5,6,9,5,10) How can I do that? 但是我想获取第一列以外的数据(例如5,6,9,5,10),该怎么办? Could you give me an idea? 你能给我个主意吗? Thanks. 谢谢。

void Hospital::readRecordsFile()
{
    fileName = "Waterbury Hospital patients records.csv";
    ifstream file(fileName);
    string value;
    vector <string> getInform;
    while(file.good())
    {
        getline(file, value);
        getInform.push_back(value);
        //getInform.erase(getInform.begin()+1);
    }

    for(int i=0;i<getInform.size();i++)
    {
        cout << getInform[i] << endl;
    }

}

You can find first separator (,) in each line and then delete all charaters before it: 您可以在每行中找到第一个分隔符(,),然后删除其前面的所有字符:

getline(file, value);
const auto pos = value.find(',');
if(pos != string::npos)
    value.erase(0, pos + 1);

If you are not sure about used separator character (,) in CSV file. 如果不确定CSV文件中使用的分隔符(,)。 You would probably do ignore all digits from the beginning of each line: 您可能会忽略每行开头的所有数字:

getline(file, value);
const auto pos = value.find_first_not_of("0123456789");
if(pos != string::npos)
    value.erase(0, pos + 1);

std::istream::ignore can be used to ignore some of the text from an input stream. std::istream::ignore可用于忽略输入流中的某些文本。

Extracts and discards characters from the input stream until and including delim . 从输入流中提取并丢弃字符,直到包括delim为止。

file.ignore(std::numeric_limits<std::streamsize>::max(), ',');

and follow it up with getline to read the rest of the line. 并通过getline进行后续操作,以阅读其余的内容。

getline(file, value);

You should split each line by ',' and then ignore the first part. 您应将每行均用','分隔,然后忽略第一部分。

Simply parse string to rich the first ',' character, then use substring from that index to end. 只需解析字符串以丰富第一个','字符,然后使用该索引中的子字符串结束即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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