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
>>will leave an end-of-line in the input buffer thatgetlinereads for the following lines. However, using thegetlineis still wrong. You could do justwhile (ifs >> etc.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).