标题:为什么读出来的输出结果多一个?求教
取消只看楼主
starlz
Rank: 2
等 级:论坛游民
帖 子:15
专家分:27
注 册:2016-10-22
结帖率:100%
已结贴  问题点数:20 回复次数:1 
为什么读出来的输出结果多一个?求教
#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
为什么呢?
搜索更多相关主题的帖子: private include public friend return 
2016-11-01 08:15
starlz
Rank: 2
等 级:论坛游民
帖 子:15
专家分:27
注 册:2016-10-22
得分:0 
谢谢楼上,我上网查了一下改成这样也可以:)
    vector<Students>s;
    Students a;
    ifstream in;
    in.open("Text.txt");
while (!in.eof())
        {
            in >> a;
            s.push_back(a);
        }
    in.close();
2016-11-01 09:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-470548-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.231894 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved