C++上机的一个题目,我读取二进制文件时所有的字符型数据变成了乱码,求大神指点!!

#include<iostream>#include<string>#include<fstream>usingnamespacestd;classstudent{pub... #include<iostream>
#include<string>
#include<fstream>
using namespace std;
class student
{
public:
student()
{}
student(int n,string na,int a,char s,float g)
{num=n;name=na;age=a;sex=s;grade=g;}
void display();
void set(int n,string na,int a,char s,float g);
int num;
private:
string name;
int age;
char sex;
float grade;
};
void student::display()
{
cout<<"NO."<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"grade:"<<grade<<endl;
}
void student::set(int n,string na,int a,char s,float g)
{
num=n;
name=na;
age=a;
sex=s;
grade=g;
}

class ClassManagement
{
public:
student stud[20];
void search();
void edit();
void sorted();
void display();
ClassManagement(int m)
{n=m;}
private:
int n;
};

void ClassManagement::search()
{
int number,i;
cout<<"please input your student's num:";
cin>>number;
for(i=0;i<n;i++)
if(number==stud[i].num)
stud[i].display();
}

void ClassManagement::edit()
{
int number,num,age,i;
string name;
char sex;
float grade;
cout<<"please input your student's num:";
cin>>number;
for(i=0;i<n;i++)
if(number==stud[i].num)
cout<<"please input your new data:"<<endl;
cout<<"NO.";cin>>num;
cout<<"name:";cin>>name;
cout<<"age:";cin>>age;
cout<<"sex:";cin>>sex;
cout<<"grade:";cin>>grade;
stud[i].set(num,name,age,sex,grade);
}
void ClassManagement::sorted()
{
int i,j;
student temp;
for(i=0;i<n-1;i++)//选择排序法
{
for(j=i+1;j<n;j++)
{
if(stud[i].num>stud[j].num)
{
temp=stud[i];
stud[i]=stud[j];
stud[j]=temp;
}
}
}
}

void ClassManagement::display()
{
int i;
for(i=0;i<n;i++)
stud[i].display();
}
int main()
{
int n;
int test_write(int n);
int test_read(int n,ClassManagement &a);
cout<<"how many students you want to record?"<<endl;
cout<<"Student's number:";
cin>>n;
ClassManagement a(n);
test_write(n);
test_read(n,a);
a.sorted();
a.display();
return 0;
}
int test_write(int n)
{
int num,age,i;
string name;
char sex;
float grade;
ClassManagement a(n);
ofstream outf("student.dat",ios::out|ios::binary);
if(!outf)
{
cout<<"Cannot open output file!"<<endl;
abort();
}
for(i=0;i<n;i++)
{
cout<<"please input student's information:"<<endl;
cout<<"NO.";cin>>num;
cout<<"name:";cin>>name;
cout<<"age:";cin>>age;
cout<<"sex:";cin>>sex;
cout<<"grade:";cin>>grade;
a.stud[i].set(num,name,age,sex,grade);
outf.write((char*)&a.stud[i],sizeof(student));
}
outf.close();
return 0;
}

int test_read(int n,ClassManagement &a)
{
int i;
ifstream inf("student.dat",ios::binary);
if(!inf)
{
cout<<"Cannot open input file!"<<endl;
abort();
}
for(i=0;i<n;i++)
inf.read((char*)&a.stud[i], sizeof(student));
inf.close();
return 0;
}
展开
 我来答
x790066846
2014-06-26 · TA获得超过790个赞
知道小有建树答主
回答量:183
采纳率:0%
帮助的人:202万
展开全部

亲测无误!

不过测试过程中,可能存在问题:

  1. 编号是不是一定得从0开始(防止数组下表越界)

  2. 所有信息都必须填完整吗(是否有默认参数值)

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式