各位!!看看好奇怪为什么写进去的文件读取时老出错,当吧数据类型由string换成int就能正常读取。。求解答
#include<iostream>#include<string>
#include<time.h>
#include<stdio.h>
#include<fstream>
using namespace std;
time_t timep;
struct tm *p;
class People
{
public:
string m_Name;
string m_Sex;
int m_Age;
int m_Hour;
int m_Mine;
int m_Sec;
public:
// People(){}//默认构造函数
People(string name="_",int age=0,string sex="_",int h=0,int m=0,int s=0):m_Name(name),m_Age(age),m_Sex(sex),m_Hour(h),m_Mine(m),m_Sec(s){}//带参的默认构造函数-形参列表-内联形式
virtual void RegistMessage();
virtual void ShowMessage();//类外定义形式
virtual ostream& operator<<(ostream& output);//输出,重载函数
~People(){}//析构函数--内联
};
ostream& People::operator <<(ostream& output)
{
puts("签到时间:");
output<<m_Hour+8<<":"<<m_Mine<<":"<<m_Sec<<endl;
return output;
}
void People::ShowMessage()
{
cout<<"姓名\t年龄\t性别"<<endl;
cout<<m_Name<<"\t"<<m_Age<<"\t"<<m_Sex<<endl;
}
void People::RegistMessage()
{
cout<<"请输入名字"<<endl;
cin>>this->m_Name;
cout<<"请输入年龄"<<endl;
cin>>this->m_Age;
if(m_Age>=100||m_Age<=15)
//throw Exception("Error!!不合法的输入操作!!");
cout<<"请输入性别"<<endl;
cin>>this->m_Sex;
time (&timep);
p=gmtime(&timep);
m_Hour=p->tm_hour;
m_Mine=p->tm_min;
m_Sec=p->tm_sec;
}
class Teacher:public People//继承和组合同用
{
string m_TeacheSb;
string m_TeacheGa;
public:
virtual void RegistMessage();
virtual void ShowMessage();//类外定义形式
string OutName();
Teacher(string name1="-",int age1=0,string sex1="_",string m_TeacherSb="_",string m_TeacheGa="_");
~Teacher(){};
};
Teacher::Teacher(string name1,int age1,string sex1,string m_TeacherSb1,string m_TeacheGa1):People(name1,age1,sex1)//注意在声明时不用写默认值
{
m_TeacheSb=m_TeacherSb1;
m_TeacheGa=m_TeacheGa1;
}
string Teacher::OutName()
{
return m_Name;
}
void Teacher::ShowMessage()
{
cout<<"姓名\t年龄\t性别\t所授科目\t所授年级"<<endl;
cout<<m_Name<<"\t"<<m_Age<<"\t"<<m_Sex<<"\t"<<m_TeacheSb<<"\t"<<m_TeacheGa<<endl;
}
void Teacher::RegistMessage()
{
puts("姓名:");
cin>>m_Name;
puts("年龄:");
cin>>m_Age;
puts("性别:");
cin>>m_Sex;
puts("请输入所授科目");
cin>>m_TeacheSb;
puts("请输入所授班级");
cin>>m_TeacheGa;
time (&timep);
p=gmtime(&timep);
m_Hour=p->tm_hour;
m_Mine=p->tm_min;
m_Sec=p->tm_sec;
}
class Student:public People
{
int m_Number;
public:
void RegistMessage();
void ShowMessage();
int OutNumber();
Student(string name="_",int age=0,string sex="_",int number=0):People(name,age,sex){m_Number=number;}
~Student(){};
};
void Student::ShowMessage()
{
cout<<"学号\t姓名\t性别\t年龄"<<endl;
cout<<m_Number<<"\t"<<m_Sex<<"\t"<<m_Name<<"\t"<<m_Age<<endl;
}
void Student::RegistMessage()
{
puts("姓名:");
cin>>m_Name;
puts("年龄:");
cin>>m_Age;
puts("性别:");
cin>>m_Sex;
puts("请输入学号");
cin>>m_Number;
time (&timep);
p=gmtime(&timep);
m_Hour=p->tm_hour;
m_Mine=p->tm_min;
m_Sec=p->tm_sec;
}
int Student::OutNumber()
{
return m_Number;
}
void WantThing(People* pe)//测试多态性
{
pe->ShowMessage();
}
void main()
{
int i;
int j;
FILE *fp_Student;
FILE *fp_Teacher;
Teacher te[2];
Student st[2];
int choose1;
int choose2;
cout<<"。。。。。。。。。。。。。。。。。欢迎使用签到系统。。。。。。。。。。。。。。。\n\n"<<endl;
cout<<"(1)查询\t\t(2学生入口\t(3)教师入口\t(4)退出"<<endl;
cin>>choose1;
switch(choose1)
{
case 1:
puts("1 学生\t 2 教师");
cin>>choose2;
switch(choose2)
{
case 1:
if((fp_Student=fopen("stu.txt","r"))==NULL)
{
cout<<"文件打开失败"<<endl;
exit(0);
}
else
{
for(i=0;i<2;i++)
{
fread(&st[i],sizeof(Student),1,fp_Student);
}
}
fclose(fp_Student);
cout<<"签到情况:"<<endl;
for(i=0;i<2;i++)
st[i].ShowMessage();
break;
case 2: if((fp_Teacher=fopen("Teacher.dat","rb"))==NULL)
{
cout<<"文件打开失败"<<endl;
exit(0);
}
else
{
for(j=0;j<2;j++)
{
fread(&st,sizeof(Teacher),1,fp_Teacher);
}
}
fclose(fp_Teacher);
cout<<"签到情况:"<<endl;
for(j=0;j<2;j++)
te[j].ShowMessage();
// st.ShowMessage();
break;
}
break;
case 2:
if((fp_Student=fopen("stu.txt","w"))==NULL)
{
cout<<"打开文件失败"<<endl;
}
else
{
for(i=0;i<2;i++)
{
st[i].RegistMessage();
fwrite(&st,sizeof(Student),1,fp_Student);
cout<<"恭喜你成功签到!!!"<<endl;
}
fclose(fp_Student);
cout<<"文件关闭!!"<<endl;
}
for(i=0;i<2;i++)
{
st[i].ShowMessage();
st[i].operator <<(cout);
}
break;
case 3:
if((fp_Teacher=fopen("Teacher.dat","wb"))==NULL)
{
cout<<"打开文件失败"<<endl;
}
else
{
for(j=0;j<2;j++)
{
te[j].RegistMessage();
fwrite(&te[j],sizeof(Teacher),1,fp_Teacher);
cout<<"恭喜你成功签到!!!"<<endl;
}
fclose(fp_Teacher);
cout<<"文件关闭!!"<<endl;
}
break;
case 4:exit(0);break;
}
}