为什么读出来的输出结果多一个?求教
#include<iostream>#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
class Students
{
private:
int stuno,classno;
string name;
float cscore, pscore, tscore;
public:
Students():stuno(0),classno(0),cscore(0),pscore(0),tscore(0){}
friend istream &operator>>(istream&, Students&);
friend ostream &operator<<(ostream&, Students&);
float avgscore(){ return (cscore + pscore + tscore) / 3; }
};
istream &operator>>(istream &is, Students &s)
{
is >> s.stuno >> s.name >> s.classno >> s.cscore >> s.pscore >> s.tscore;
return is;
}
ostream &operator<<(ostream &os, Students &s)
{
os << s.stuno << " " << s.name << " " << s.classno << " " << s.cscore << " " << s.pscore << " " << s.tscore << endl;
return os;
}
int main()
{
vector<Students>s;
Students a;
ifstream in;
in.open("Text.txt");
while (in)
{
in >> a;
s.push_back(a);
}
in.close();
ofstream out("Text1.txt");
for (vector<Students>::iterator it = s.begin(); it != s.end(); ++it)
{
out << *it << endl;
}
out.close();
return 0;
}
输入:1 lz 4 99 99 99
2 lx 4 98 98 98
3 wl 4 97 97 97
输出:
1 lz 4 99 99 99
2 lx 4 98 98 98
3 wl 4 97 97 97
3 wl 4 97 97 97
为什么呢?