各位大侠,帮帮看看这个学生管理系统,能运行,可是出错
#include<iostream>#include<fstream>
#include<cstring>
using namespace std;
struct student
{char name[30];
int num;
int score;
};
int i=0;
const student mark={"noname\0",0,0};
const char *filetext="d:\\student.txt";
const char *filename="d:\\student.dat";
void cucun(char *file)
{char choice;
student stu;
fstream outstuf(file,ios::in|ios::out|ios::binary);
outstuf.open(file,ios::out);
cout<<"如果文件存在,将删除所有数据,要进行初始化吗?(Y/N)"<<endl;
cin>>choice;
if(choice=='Y'||choice=='y')
{outstuf.seekp(0,ios::beg);
outstuf.write((char*) &mark,sizeof(student));
cout<<"初始化成功"<<endl;
}
else
cout<<"取消操作"<<endl;
cout<<"学生数"<<endl;
cin>>i;
cout<<"输入学生的相关信息\n"<<endl;
while(i)
{cout<<"学号"<<endl;
cin>>stu.num;
cout<<"姓名"<<endl;
cin>>stu.name;
cout<<"成绩"<<endl;
cin>>stu.score;
outstuf.write((char*) &stu,sizeof(student));
i--;
}
outstuf.write((char*) &mark,sizeof(student));
outstuf.close();
}
int endmark(student stu)
{if(stu.num==0)
{printf("找不到记录\n");
return 1;}
return 0;
}
void chakan(char *file,char *text)
{char s[80];
student stu;
fstream instuf(file,ios::in|ios::binary);
fstream outstuf(text,ios::out);
instuf.seekp(0,ios::beg);
cout<<"*******************建立文本文件*******************"<<endl;
do
{instuf.read((char*)&stu,sizeof(student));
if(!endmark(stu))
outstuf<<stu.name<<'\t'<<stu.num<<'\t'<<stu.score<<endl;
}while(!endmark(stu));
outstuf.close();
cout<<"***********文件建立*****************"<<endl;
outstuf.open(text,ios::in);
while(!outstuf.eof() )
{
outstuf.getline(s,80);
cout<<s<<endl;
}
outstuf.close();
instuf.close();
}
void tongji(char *file)
{student stu;
int total,max,min;
double ave;
int n=0;
max=min=0;
ifstream instuf(file,ios::in|ios::out|ios::binary);
instuf.seekg(0,ios::beg);
while(i)
{instuf.read((char*) &stu,sizeof(student));
if(stu.score>max)
max=stu.score;
if(stu.score<min)
min=stu.score;
total+=stu.score;
i--;
}
cout<<max<<min<<total<<endl;
instuf.close();
}
void xuehao(char *file)
{ifstream instuf;
int key;
student stu;
ifstream(filename,ios::in|ios::out|ios::binary);
instuf.seekg(0,ios::beg);
cout<<"please input the number which you want know\n";
cin>>key;
do
{instuf.read((char*) &stu,sizeof(student));
}while(stu.num!=key&&!endmark(stu));
if(stu.num==key)
cout<<stu.name<<'\t'<<stu.num<<'\t'<<stu.score<<endl;
instuf.close();
}
void searchname(char *file)
{char s[30];
ifstream instuf;
student stu;
ifstream(file,ios::in|ios::out|ios::binary);
cout<<"please input the number which you want know\n";
cin>>s;
do
{instuf.read((char*) &stu,sizeof(student));
}while(strcmp(s,stu.name)!=0 &&!endmark(stu));
if(strcmp(s,stu.name)==0)
cout<<stu.name<<'\t'<<stu.num<<'\t'<<stu.score<<endl;
instuf.close();
}
void search(char *f)
{char choice;
cout<<"*************chazhao**********\n"
<<"1:xingming\t"
<<"2:xuehao\n";
switch(choice)
{case '1':searchname((char*)f);break;
case '2': xuehao((char* )f);break;
default:cout<<"输入错误,请重新输入\n"<<endl;
}
}
void xiugai(char *file)
{
student stu;
int key,choice;
fstream instuf(file,ios::in|ios::out|ios::binary);
instuf.seekg(0,ios::beg);
cout<<"书号"<<endl;
cin>>key;
do
{instuf.read((char*) &stu,sizeof(student));
}while(stu.num!=key && endmark(stu));
if(stu.num==key)
{cout<<"输入修改的score"<<endl;
cin>>choice;
stu.score=choice;
}
else
{cout<<"error"<<endl;
}
instuf.seekg(-long(sizeof(student)));
instuf.write((char *)&stu,sizeof(student));
instuf.close();
}
void zengjia(char *file)
{
student stu;
fstream instuf(file,ios::in|ios::out|ios::binary);
instuf.seekg(0,ios::beg);
do
{instuf.read((char*)&stu,sizeof(student));
}while(!endmark(stu));
instuf.seekp(-long(sizeof(student)),ios::cur);
cout<<"名字 学号 成绩"<<endl;
cin>>stu.name;
cin>>stu.num;
cin>>stu.score;
instuf.write((char *) &stu,sizeof(student));
instuf.write((char *)&mark,sizeof(student));
i++;
instuf.close();
}
int main()
{char choice;
while(1)
{cout<<"****************学生管理系统\n************请选择操作\n"
<<"1、储存\n"
<<"2、查看\n"
<<"3、统计\n"
<<"4、查找\n"
<<"5、修改\n"
<<"6、增加\n"<<endl;
cin>>choice;
switch(choice)
{case '1':cucun((char*)filename);break;
case '2':chakan((char*)filename,(char*) filetext);break;
case '3':tongji((char*)filename);break;
case '4':search((char*)filename);break;
case '5':xiugai((char*)filename);break;
case'6':zengjia((char*)filename);break;
case 'Q':cout<<"退出"<<endl;return 0;
default:cout<<"输入错误,请重新输入\n"<<endl;
}
}
}