0

Currently reading in a file with about 42,000 records in it. Although my vectors seem to always come up with 21,000. Going through debugging it seems to be skipping duplicate values. Like in the data example, there are 3 59's in column three. The run through input only has one then goes to 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(); 

}

Rob
  • 169
  • 1
  • 3
  • 12
  • 5
    I imagine the sample input is missing some commas? Also, the calls to `infile.ignore(',',',')` are probably not what you want... that means to ignore until a comma is found but at most `(int)','` characters... – David Rodríguez - dribeas Sep 02 '13 at 18:34
  • 1
    @DavidRodríguez-dribeas: And I imagine reading every other line into `STRING` and every other line into `tempXxx` fields would have something to do with missing fields. – Mats Petersson Sep 02 '13 at 18:42
  • I imagine [reviewing this question](http://stackoverflow.com/questions/415515/how-can-i-read-and-manipulate-csv-file-data-in-c), particularly the *second* answer, would be fairly informative. – WhozCraig Sep 02 '13 at 18:47

1 Answers1

3

You have this:

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

which reads one line into STRING

then this:

{
    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, ',');      

which reads a bunch of fields separated by commas.

The result, as far as I can see, is that every other line is read into STRING, and every other into tempXxx fields.

Did perhaps mean to do something like:

stringstream ss(STRING);
ss.ignore(100, ',');
getline(ss, tempQuarter, ',');
... 
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • This answers the question. Thanks alot guys. Coming from C#, c++ file readers are a whole different beast to me. – Rob Sep 02 '13 at 18:50