永发信息网

c++程序设计

答案:1  悬赏:0  手机版
解决时间 2021-04-07 22:01
  • 提问者网友:火车头
  • 2021-04-07 02:58
c++程序设计
最佳答案
  • 五星知识达人网友:夜风逐马
  • 2021-04-07 03:59
#include
#include
#include
struct Student {
std::string name, id, sex;
};
std::ostream& operator << (std::ostream& os, const Student& s)
{
os << "姓名:" << s.name << std::endl
<< "学号:" << s.id << std::endl
<< "性别:" << s.sex;
return os;
}
std::istream& operator >> (std::istream& is, Student& s)
{
is >> s.name >> s.id >> s.sex;
if (!is) {
s = Student();
std::cerr << "输入错误,停止输入" << std::endl;
}
return is;
}

//出题的人没吃药么…直接输出到屏幕上不就好了…还要放到文件里再读出来
std::istream& readInFile(std::ifstream& ifile, Student& s)
{
//假设数据都是按输出格式的(没有人作死的乱改)
std::string content;
auto extr = [&content]()->const std::string& {
std::string::size_type pos = content.find(':');
content = content.substr(pos+1,
content.size()-(pos+1));
return content;
};
//读取姓名
ifile >> content;
s.name = extr();
//读取学号
ifile >> content;
s.id = extr();
//读取性别
ifile >> content;
s.sex = extr();
return ifile;
}
int main()
{
std::ofstream ofile("stureginfo.txt");
Student s;
while (std::cin >> s) {
if (s.name == "0" && s.id == "0" && s.sex == "0")
break;
ofile << s << std::endl << std::endl;
}
std::ifstream ifile("stureginfo.txt");
while (readInFile(ifile, s)) {
std::cout << s << std::endl << std::endl;
}
}追问.......没写全啊,哥们
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯