文件I / O跳过重复值?

时间:2013-09-02 18:32:04

标签: c++

目前正在阅读包含约42,000条记录的文件。虽然我的矢量似乎总是拿出21,000。经过调试似乎正在跳过重复的值。与数据示例中一样,第三列中有3 59个。运行输入只有一个然后转到58。

Data Example:
20070906_NO@IND 1   59  55  IND NO  1   10  68
20070906_NO@IND 1   59  49  IND NO  2   10  68
20070906_NO@IND 1   59  15  IND NO  3   3   61
20070906_NO@IND 1   58  55  IND NO  3   8   66
20070906_NO@IND 1   58  49  IND NO  4   8   66


{
ifstream infile;
infile.open ("2007.csv");

while (infile.fail())
{
    cout << "Invalid File Name, Please Try Again. /n Filename: ";
    cin >> FileName;
    infile.open(FileName);
}

while(!infile.eof())
{       
    while (getline(infile,STRING, '\n'))
    {
        infile.ignore(',', ',');
        getline(infile,tempQuarter, ',');
        getline(infile,tempMinuteRemaining, ',');
        infile.ignore(',', ',');
        getline(infile,tempOffenseName, ',');
        getline(infile,tempDefenseName, ',');
        getline(infile,tempDown, ',');
        getline(infile,tempYardToGo, ',');
        getline(infile,tempNextYardLocation, ',');      

                    //Assign data to Vectors
        AssignToVector(tempQuarter,tempMinuteRemaining,tempOffenseName, tempDefenseName, tempDown, tempYardToGo, tempNextYardLocation, tempPlayDescription);
    }
}
cout << "Reading Completed"<<endl;  
infile.close(); 

}

1 个答案:

答案 0 :(得分:3)

你有这个:

while (getline(infile,STRING, '\n'))

将一行读入STRING

然后这个:

{
    infile.ignore(',', ',');
    getline(infile,tempQuarter, ',');
    getline(infile,tempMinuteRemaining, ',');
    infile.ignore(',', ',');
    getline(infile,tempOffenseName, ',');
    getline(infile,tempDefenseName, ',');
    getline(infile,tempDown, ',');
    getline(infile,tempYardToGo, ',');
    getline(infile,tempNextYardLocation, ',');      

读取由逗号分隔的一堆字段。

据我所知,结果是每一行都被STRING读入,而其他所有行都被tempXxx字段读入。

或许意味着做一些事情:

stringstream ss(STRING);
ss.ignore(100, ',');
getline(ss, tempQuarter, ',');
...