您的位置:首页 > 其它

第十五周上机项目一用二进制文件处理学生成绩

2015-06-17 08:18 190 查看
/*
*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;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: