十人打架真心没看

本文介绍了一个使用C++保存和读取二进制文件的示例,包括构造函数的实现和文件操作流程。通过实例演示了如何在C++中进行数据的持久化存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

* 程序的版权和版本声明部分

* 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;

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值