/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称:score.cpp
* 作 者: 刘元龙
* 完成日期: 2013 年56月 23 日
* 版本号: v1.0
* 输入描述:
* 问题描述:保存二进制文件
* 输出:
*/
#include <iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
class student
{
private:
int num; //学号
string nume; //姓名
double c; //c++
double math; //数学
double english;//英语
double sum; //总分
public:
student();
student(int n,string nu,double c,double m,double e);//构造函数,用于赋值和计算总成绩
double getnum(){return num;}
string getnume(){return nume;}
double getc(){return c;}
double getenglish(){return english;}
double getsum(){return sum;}
void stud(int n,string nu,double c,double m,double e);
};
void student::stud(int n,string nu,double c,double m,double e)
{
num=n;
nume=nu;
c=c;
math=m;
english=e;
sum=c+math+english;
}
student::student()
{
num=0000000000;
c=0;
math=0;
english=0;
}
student::student(int n,string nu,double c,double m,double e)
{
num=n;
nume=nu;
c=c;
math=m;
english=e;
sum=c+math+english;
}
int main()
{
student stu[100];
int n;
string nu;
double m,c,e;
fstream iofile("binary_score.dat",ios::in|ios::out|ios::binary);
if(!iofile)
{
cout<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<101;i++)
{
iofile>>n>>nu>>m>>c>>e;
stu[i].stud(n,nu,m,c,e);
}
infile.close();
ofstream outfile("binary_score.dat",ios::in|ios::out|ios::binary);
if(!outfile)
{
cout<<"open error!"<<endl;
exit(1);
}
for(int i=0;i<101;i++)
{
outfile.read((char *)&stu[i],sizeof(stu[i]));
}
//添加自己的信息
cout<<"请输入您的信息:";
cin>>n>>nu>>m>>c>>e;
student s(n,nu,m,c,e);
outfile.write((char *)&s,sizeof(s));
outfile.close();
return 0;
}
08-10
05-27