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;
} 展开
#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;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询