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

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

  • 写回答

3条回答 默认 最新

  • 关注

    (1)score.dat文件用记事本打开,选择“文件” -- "另存为",再另存为对话框的编码中,选择ANSI,点击保存。

    img


    (2)你的代码有点问题,读文件的时候需要记录读取的条数,写文件的时候不要写死100,代码修改了一下,运行结果如下:

    img

    代码修改如下:

    #define _CRT_SECURE_NO_WARNINGS 1
    #include <iostream>
    #include <fstream>
    #include <string>
    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;
            return 0;
        }
        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);
        }
        int size = 0;
        for ( size = 0; !f1.eof(); size++) {
            f1 >> Id >> Name >> Cpp >> Math >> English;
            num[size].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 < size; 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)) )
                cout << x.get_id() << " " << x.name << " " << x.get_cpp() << " " << x.get_math() << " " << x.get_english() << " " << x.get_sum() << endl;
        }
        f3.close();
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥15 关于#java#的问题,请各位专家解答!(开发工具-eclipse)
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题