| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1198 人关注过本帖
标题:为什么读出来的输出结果多一个?求教
取消只看楼主 加入收藏
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
快速回复:为什么读出来的输出结果多一个?求教
数据加载中...
 
   



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

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