📄 save_lord.cpp
字号:
#include "cpp9.h"
/********************
* 文件存储操作函数
********************/
void Student::save(Student *top)
{
if (top->name[0]=='0')
{
cout << "\t没有记录可存!" << endl;
return;
}
ofstream out;
out.open("stud.dat",ios::out);
if (!out)
{
cout << "\t不能打开文件!" << endl;
exit(1);
}
cout << "\n\t存文件" << endl;
out << top->count << '\n'; //重载运算函数保存学生信息
Student *p=top;
while(p!=NULL)
{
out <<*p;
/*
int j=0;
while(1)
{
if(p->co[j].Getname()[0]=='0')break;
out.write((char*)&p->co[j],sizeof p->co[j]);
j++;
}*/
p=p->next;
}
out.close();
cout << top->count << "\t条记录已经存入文件,请继续操作。" << endl;
}
/********************
* 文件读取操作函数
********************/
Student * Student::load(Student *top)
{
ifstream in;
in.open("stud.dat",ios::in|ios::nocreate);
if (!in)
{
cout << "\t文件不存在!" << endl;
return top;
}
cout << "\n\t取文件..." << endl;
in >> top->count;
in.ignore();
Student *p = top;
Student *old;
for (int i=0; i<top->count; i++)
{
in >> *p;
/*int j=0;
while(1)
{
if(p->co[j].Getname()[0]=='0')break; //
in.read((char*)&p->co[j],sizeof p->co[j]);
j++;
}*/
ASK(p->next);
old=p;
p=p->next;
in.ignore();
}
old->next=NULL;
in.close();
cout << "\t取入" << top->count << "条记录。" << endl;
return(top);
}
/********************
* 重载>>运算符函数
********************/
istream & operator >> (istream &is,Student &ob)
{
is.getline(ob.no,12,'\n'); //使其能够读入字符串中的空格
is.getline(ob.name,16,'\n');is.ignore(); //使其能够读入字符串中的空格
is.getline(ob.pw.pwo,10,'\n');
is.getline(ob.co[0].name,10,'\n');
is>>ob.co[0].credit ;
is>>ob.co[0].score ;is.ignore();
is.getline(ob.co[1].name ,10,'\n');
is>>ob.co[1].credit ;
is>>ob.co[1].score ;is.ignore();
is.getline(ob.co[2].name ,10,'\n');
is>>ob.co[2].credit ;
is>>ob.co[2].score ;is.ignore();
is.getline(ob.co[3].name,10,'\n');
is>>ob.co[3].credit;
is>>ob.co[3].score;
is >>ob.ave;
return is;
}
/********************
* 重载<<运算符函数
********************/
ostream & operator << (ostream &os,Student &ob)
{
os << ob.no << '\n';
os << ob.name << '\n';
os<<ob.pw.pwo<<'\n';
os<<ob.co[0].name<<'\n';
os<<ob.co[0].credit<<'\n';
os<<ob.co[0].score<<'\n';
os<<ob.co[1].name<<'\n';
os<<ob.co[1].credit<<'\n';
os<<ob.co[1].score<<'\n';
os<<ob.co[2].name<<'\n';
os<<ob.co[2].credit<<'\n';
os<<ob.co[2].score<<'\n';
os<<ob.co[3].name<<'\n';
os<<ob.co[3].credit<<'\n';
os<<ob.co[3].score<<'\n';
os << ob.ave<<'\n';
return os;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -