m0_70323518 2022-05-16 21:43 采纳率: 100%
浏览 41
已结题

C++文件输入与读取

c++二进制文件无法读取正常
include
#include
#include
#include
using namespace std;

class Student{
public:
Student(){
}
Student(int id,char n[],double cpp,double math,double english){
this->id=id;
strcpy(name,n);
this->cpp=cpp;
this->math=math;
this->english=english;
sum=cpp+math+english;
}
double set(int id,char n[],double cpp,double math,double english){
this->id=id;
strcpy(name,n);
this->cpp=cpp;
this->math=math;
this->english=english;
sum=cpp+math+english;
}
int get_id(){
return id;
}

    double get_cpp(){
        return cpp;
    }
    double get_math(){
        return math;
    }
    double get_english(){
        return english;
    }
    double get_sum(){
        return sum;
    }

    int id;
    char name[100];
    double cpp;
    double math;
    double english;
    double sum;

};
int main(){
Student num[101];
int Id;
double English,Math,Cpp;
char Name[100];
char n[100];
fstream f1;
f1.open("score.dat",ios::in);
if(f1.fail()){
cout<<"打开文件失败!"<<endl;
exit(1);
}
for(int i=0;i<100;i++){
f1>>Id>>Name>>Cpp>>Math>>English;
num[i].set(Id,Name,Cpp,Math,English);
}
f1.close();
fstream f2;
f2.open("binary_score.dat",ios::out|ios::binary);
if(f2.fail()){
cout<<"打开文件失败!"<<endl;
exit(2);
}
for(int i=0;i<100;i++){
f2.write((char*)&num[i],sizeof(num[i]));
}
cout<<"请输入信息:"<<endl;
cin>>Id>>Name>>Cpp>>Math>>English;
Student s(Id,Name,Cpp,Math,English);
f2.write((char*)&s,sizeof(s));
f2.close();
Student x;
fstream f3;
f3.open("binary_score.dat",ios::in|ios::binary);
if(f3.fail()){
cout<<"打开文件失败!"<<endl;
exit(3);
}
while(!f3.eof()){
f3.read((char*)&x,sizeof(x));
cout<<x.get_id()<<" "<<x.name<<" "<<x.get_cpp()<<" "<<x.get_math()<<" "<<x.get_english()<<" "<<x.get_sum()<<endl;
}
f3.close();
return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 关注

    我测试正常啊

    img

    img

    #include <iostream>
    #include <cstring>
    #include <fstream>
    using namespace std;
    
    class Student
    {
    public:
        Student()
        {
        }
        Student(int id, char n[], double cpp, double math, double english)
        {
            this->id = id;
            strcpy(name, n);
            this->cpp = cpp;
            this->math = math;
            this->english = english;
            sum = cpp + math + english;
        }
        void set(int id, char n[], double cpp, double math, double english)
        {
            this->id = id;
            strcpy(name, n);
            this->cpp = cpp;
            this->math = math;
            this->english = english;
            sum = cpp + math + english;
        }
        int get_id()
        {
            return id;
        }
    
        double get_cpp()
        {
            return cpp;
        }
        double get_math()
        {
            return math;
        }
        double get_english()
        {
            return english;
        }
        double get_sum()
        {
            return sum;
        }
    
        int id;
        char name[100];
        double cpp;
        double math;
        double english;
        double sum;
    };
    int main()
    {
        Student num[101];
        int Id;
        double English, Math, Cpp;
        char Name[100];
        char n[100];
        fstream f1;
        f1.open("score.dat", ios::in);
        if (f1.fail())
        {
            cout << "打开文件失败!" << endl;
            exit(1);
        }
        for (int i = 0; i < 3; i++)
        {
            f1 >> Id >> Name >> Cpp >> Math >> English;
            num[i].set(Id, Name, Cpp, Math, English);
        }
        f1.close();
        fstream f2;
        f2.open("binary_score.dat", ios::out | ios::binary);
        if (f2.fail())
        {
            cout << "打开文件失败!" << endl;
            exit(2);
        }
        for (int i = 0; i < 3; i++)
        {
            f2.write((char *)&num[i], sizeof(num[i]));
        }
        cout << "请输入信息:" << endl;
        cin >> Id >> Name >> Cpp >> Math >> English;
        Student s(Id, Name, Cpp, Math, English);
        f2.write((char *)&s, sizeof(s));
        f2.close();
        Student x;
        fstream f3;
        f3.open("binary_score.dat", ios::in | ios::binary);
        if (f3.fail())
        {
            cout << "打开文件失败!" << endl;
            exit(3);
        }
        while (!f3.eof())
        {
            if(!f3.read((char *)&x, sizeof(x)))
                break;
            cout << x.get_id() << " " << x.name << " " << x.get_cpp() << " " << x.get_math() << " " << x.get_english() << " " << x.get_sum() << endl;
        }
        f3.close();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月16日

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?