简体   繁体   中英

Reading file in C++ through ifstream object

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

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);
    [..]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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