0

I have a program written for reading files using ifstream object.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct FPlayerInfo
{
    string name;
    string pos;
    int numTouchDowns;
    int numCatches;
    int numPassingYards;
    int numReceivingYards;
    int numRushingYards;
};


void loadInfo(FPlayerInfo info[10], string filename)
{
    int i=0;
    ifstream ifs(filename.c_str() , ios::in);

    if (ifs.good() && ifs.is_open())
    {
        string line;
        while (getline(ifs, line))
        {
            ifs >> info[i].name >> info[i].pos >> info[i].numTouchDowns >> info[i].numCatches >> info[i].numPassingYards 
                >> info[i].numReceivingYards >> info[i].numRushingYards;

            cout << info[i].name << endl;
            i++;
        }

        ifs.close();
    }
}

int main()
{
    FPlayerInfo info[10];

    loadInfo(info , "Footballdata.txt"); 

    return 0;  
}

The Footballdata.txt contains the following data:

Bill Quarter_Back 70 0 8754 0 573
Jackson Receiver 55 87 50 5490 574
Grahm Running_Back 45 30 0 50 2800
McCoy Full_Back 25 10 0 25 3762
Daryl Quarter_Back 50 2 7560 0 450
Santiago Left_Tackle 5 0 0 0 0
Hanks Receiver 35 37 0 3590 876
Johnson Running_Back 25 80 0 100 4000
Miller Receiver 110 250 150 7867 2100
Ruth Quarter_Back 85 0 12901 0 3249

After execution of the above program, I expect all the names to be returned in the output. But What I observed is that the first name is missing in the actual output and last line I am getting blank. Can anyone please tell me where I am making mistake while reading the file ?

Thanks in advance.

Actual Output:

Jackson
Grahm
McCoy
Daryl
Santiago
Hanks
Johnson
Miller
Ruth
4
  • No , its not like that , only first line is missing. Commented Oct 26, 2017 at 8:46
  • 1
    Yes, >> will leave an end-of-line in the input buffer that getline reads for the following lines. However, using the getline is still wrong. You could do just while (ifs >> etc. Commented Oct 26, 2017 at 8:48
  • @BoPersson, How will I know about the end of file ? Can you tell me complete statement in while loop ? Commented Oct 26, 2017 at 8:49
  • A bit ugly, but you can put the entire input line inside the while(). It will return true if all the input worked and false when it fails. (Assuming that end of file is the only place where input fails). Commented Oct 26, 2017 at 8:51

1 Answer 1

1

getline reads a line from the input and then ifs >> info[i].name >> .. reads additional info thus skipping what you have read using getline.

Simply change:

string line;
while (getline(ifs, line))
{
    ifs >> info[i].name >> info[i].pos >> info[i].numTouchDowns >> info[i].numCatches >> info[i].numPassingYards 
        >> info[i].numReceivingYards >> info[i].numRushingYards;
    [..]
}

Into:

while (true)
{
    ifs >> info[i].name >> info[i].pos >> info[i].numTouchDowns >> info[i].numCatches >> info[i].numPassingYards 
        >> info[i].numReceivingYards >> info[i].numRushingYards;
    if (!ifs) break;
    [..]
}

An better implementation (splitted to avoid littering the actual issue), is using a temp object and moving it once we realized the read was successful:

while (true)
{
    FPlayerInfo temp;
    ifs >> temp.name >> temp.pos >> temp.numTouchDowns >> temp.numCatches >> temp.numPassingYards 
        >> temp.numReceivingYards >> temp.numRushingYards;
    if (!ifs) break;

    info[i] = std::move(temp);
    [..]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Getting some garbage values at the end though it is reading the first line now.
@BhawandeepSingla, added an improvement recommendation, please take a look :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.