/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:陈栋梁
*完成日期: 2015 年 6 月 17 日
*版本号:v1.0
*
*
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
const int Num=100;
class student
{
private:
string number;
string name;
double cpp;
double math;
double english;
double total;
public:
student () {}
void getdata(string,string,double,double,double,double);
void outdata();
};
void student::getdata(string num,string n,double c,double m,double e,double t)
{
number=num;
name=n;
cpp=c;
math=m;
english=e;
total=t;
}
void student::outdata()
{
cout<<number<<" "<<name<<" "<<cpp<<" "<<math<<" "<<english<<" "<<total<<endl;
}
int main()
{
student s[Num],my,S;
string num,na;
double c,m,e,t;
int i;
ifstream infile("score.dat",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0; i<Num; ++i)
{
infile>>num>>na>>c>>m>>e;
t=c+m+e;
s[i].getdata(num,na,c,m,e,t);
}
infile.close();
ofstream outfile("binary_score.dat",ios::out|ios::binary);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0; i<Num; ++i)
{
outfile.write((char*)&s[i],sizeof(s[i]));
}
cout<<"输入信息:"<<endl;
cin>>num>>na>>c>>m>>e;
t=c+m+e;
my.getdata(num,na,c,m,e,t);
outfile.write((char*)&my,sizeof(my));
outfile.close();
ifstream in("binary_score.dat",ios::in|ios::binary);
if(!in)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!in.eof())
{
in.read((char*)&S,sizeof(S));
S.outdata();
}
in.close();
return 0;
}
运行结果: