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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错