c++程序 类与二进制文件的读写问题
#include<iostream>#include<string>#include<iomanip>#include<fstream>usingnamespacestd...
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Stud //类
{
private:
string xuehao;
string name;
string kecheng;
string jieci;
string reason;
int date[3];
static int num;
public:
string getxuehao(){return xuehao;}
string getname(){return name;}
string getkecheng(){return kecheng;}
string getjieci(){return jieci;}
string getreason(){return reason;}
int *getdate(){int *p=date;return p;}
static int getnum(){return num;}
void setxuehao(string x){xuehao=x;}
void setname(string n){name=n;}
void setkecheng(string k){kecheng=k;}
void setjieci(string j){jieci=j;}
void setreason(string r){reason=r;}
void setdate(int y,int m,int d){date[0]=y;date[1]=m;date[2]=d;}
void print_date(){cout<<date[0]<<"年"<<date[1]<<"月"<<date[2]<<"日";}
static void num_p(){num++;}
static void num_m(){num--;}
void print_stu();
void print_cla();
void print_all();
};
int Stud::num=0;
Stud st[20];
void caidan();
void shuru();
void xiugai();
void chaxun_cla();
void chaxun_stu();
void chaxun_all();
int main() //主函数
{
int a=1;
while(a)
{
caidan();
cin>>a;
switch(a)
{
case(0):exit(0);break;
case(1):shuru();break;
case(2):xiugai();break;
case(3):chaxun_stu();break;
case(4):chaxun_cla();break;
case(5):chaxun_all();break;
default:cout<<"error\n\n";break;
}
}
return 0;
}
void shuru() //输入函数
{
fstream iofile("Information.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{
cerr<<"文件打开错误!"<<endl<<endl;
abort();
}
int w=0;
while(iofile.read((char*)&st[w],sizeof(st[0])))
w++;
int z=1;
while(z==1)
{
int i=Stud::getnum();
int a=0,b=0,c=0;
string q;
cout<<"请输入学生姓名"<<endl;
cin>>q;
st[i].setname(q);
cout<<"请输入学生学号"<<endl;
cin>>q;
st[i].setxuehao(q);
cout<<"请输入日期:年"<<endl;
cin>>a;
cout<<"请输入日期:月"<<endl;
cin>>b;
cout<<"请输入日期:日"<<endl;
cin>>c;
st[i].setdate(a,b,c);
cout<<"请输入课程名称"<<endl;
cin>>q;
st[i].setkecheng(q);
cout<<"请输入课程节次"<<endl;
cin>>q;
st[i].setjieci(q);
cout<<"请输入缺课类型"<<endl;
cin>>q;
st[i].setreason(q);
cout<<"输入完成!是否继续输入?(1.继续输入 2.返回菜单)"<<endl;
cin>>z;
Stud::num_p();
}
for(w=0;w<Stud::getnum();w++)
{
iofile.write((char*)&st[w],sizeof(st[w]));
}
if(!iofile.write((char*)&st[w],sizeof(st[w])))
{
cerr<<"文件写入错误!"<<endl<<endl;
}
cout<<endl<<endl;
iofile.close();
}
已经有Information.dat文件,但是打开和写入出错
编译器vc++6.0
求大神解答,急 展开
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class Stud //类
{
private:
string xuehao;
string name;
string kecheng;
string jieci;
string reason;
int date[3];
static int num;
public:
string getxuehao(){return xuehao;}
string getname(){return name;}
string getkecheng(){return kecheng;}
string getjieci(){return jieci;}
string getreason(){return reason;}
int *getdate(){int *p=date;return p;}
static int getnum(){return num;}
void setxuehao(string x){xuehao=x;}
void setname(string n){name=n;}
void setkecheng(string k){kecheng=k;}
void setjieci(string j){jieci=j;}
void setreason(string r){reason=r;}
void setdate(int y,int m,int d){date[0]=y;date[1]=m;date[2]=d;}
void print_date(){cout<<date[0]<<"年"<<date[1]<<"月"<<date[2]<<"日";}
static void num_p(){num++;}
static void num_m(){num--;}
void print_stu();
void print_cla();
void print_all();
};
int Stud::num=0;
Stud st[20];
void caidan();
void shuru();
void xiugai();
void chaxun_cla();
void chaxun_stu();
void chaxun_all();
int main() //主函数
{
int a=1;
while(a)
{
caidan();
cin>>a;
switch(a)
{
case(0):exit(0);break;
case(1):shuru();break;
case(2):xiugai();break;
case(3):chaxun_stu();break;
case(4):chaxun_cla();break;
case(5):chaxun_all();break;
default:cout<<"error\n\n";break;
}
}
return 0;
}
void shuru() //输入函数
{
fstream iofile("Information.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{
cerr<<"文件打开错误!"<<endl<<endl;
abort();
}
int w=0;
while(iofile.read((char*)&st[w],sizeof(st[0])))
w++;
int z=1;
while(z==1)
{
int i=Stud::getnum();
int a=0,b=0,c=0;
string q;
cout<<"请输入学生姓名"<<endl;
cin>>q;
st[i].setname(q);
cout<<"请输入学生学号"<<endl;
cin>>q;
st[i].setxuehao(q);
cout<<"请输入日期:年"<<endl;
cin>>a;
cout<<"请输入日期:月"<<endl;
cin>>b;
cout<<"请输入日期:日"<<endl;
cin>>c;
st[i].setdate(a,b,c);
cout<<"请输入课程名称"<<endl;
cin>>q;
st[i].setkecheng(q);
cout<<"请输入课程节次"<<endl;
cin>>q;
st[i].setjieci(q);
cout<<"请输入缺课类型"<<endl;
cin>>q;
st[i].setreason(q);
cout<<"输入完成!是否继续输入?(1.继续输入 2.返回菜单)"<<endl;
cin>>z;
Stud::num_p();
}
for(w=0;w<Stud::getnum();w++)
{
iofile.write((char*)&st[w],sizeof(st[w]));
}
if(!iofile.write((char*)&st[w],sizeof(st[w])))
{
cerr<<"文件写入错误!"<<endl<<endl;
}
cout<<endl<<endl;
iofile.close();
}
已经有Information.dat文件,但是打开和写入出错
编译器vc++6.0
求大神解答,急 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询