C ++ 二进制文件处理抛出异常 0x006787EA

问题描述

我正在尝试将我的用户定义结构的对象读取和写入二进制文件。我的程序将我的对象写入文件,然后从文件中读取它,但最后它抛出异常 0x006787EA 并且我的屏幕卡住了然后我必须强行关闭visual studio。请任何人都可以发现我在做的错误

This is the exception it is throwing

This is the output

这是代码

#include<iostream>
#include<fstream>
using namespace std;
struct Student {
    int roll_no;
    string name;
};
int main() {
    ofstream wf("student.dat");
    if (!wf) {
        cout << "Cannot open file!" << endl;
        return 1;
    }
    Student wstu[3];
    wstu[0].roll_no = 1;
    wstu[0].name = "mm";
    wstu[1].roll_no = 2;
    wstu[1].name = "Sasd";
    wstu[2].roll_no = 3;
    wstu[2].name = "sdasda";
    for (int i = 0; i < 3; i++)
        wf.write(reinterpret_cast<char*>(&wstu[i]),sizeof(Student));
    wf.close();
    if (!wf.good()) {
        cout << "Error occurred at writing time!" << endl;
        return 1;
    }
    ifstream rf("student.dat");
    if (!rf) {
        cout << "Cannot open file!" << endl;
        return 1;
    }
    Student rstu[3];
    for (int i = 0; i < 3; i++)
        rf.read(reinterpret_cast<char*>(&rstu[i]),sizeof(Student));
    rf.close();
    if (!rf.good()) {
        cout << "Error occurred at reading time!" << endl;
        return 1;
    }
    cout << "Student's Details:" << endl;
    for (int i = 0; i < 3; i++) {
        cout << "Roll No: " << wstu[i].roll_no << endl;
        cout << "Name: " << wstu[i].name << endl;
        cout << endl;
    }
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)