结构体在流文件中的输入与输出

题目:
编写并测试程序:(输入输出流的write、read函数程序)
首先将3名学生的学号、姓名和成绩写入二进制文件studentinfo.dat中,然后将该文件中的数据读到结构体数组中,并将其输出到屏幕上
我的代码:

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct Student {
    int id;
    char name[20];
    double score;
};
int main() {
    ofstream ofs2;
    ofs2.open("studentinfo.dat", ios::binary |ios::out);
    if (!ofs2.is_open()) {//属于bool函数类型
        cout << "打开输入文件失败";
    }
    Student students[3] = { {101,"Alice",90.5},{102,"Bob",85.0} ,{103,"Charlie",92.0} };
    int i = 0;
    for ( i = 0; i < 3; i++) {
        ofs2.write((char*)&students[i], sizeof(Student));
        //A.write(char*buf,int cout)
        //A:文件描述字相连的文件
        // buf指针,数目
        //write(ofs2,(char*)&students[i], sizeof(Student));
    }
        //reinterpret_cast<char*>students
    ofs2.close();
    ifstream ifs2;
    ifs2.open("studentinfo.dat", ios::binary |ios::in);
    if (!ifs2.is_open()) {
        cout << "打开输出文件失败";
    }
    int j = 0;
    for ( j = 0; j < 3; j++) {
        ifs2.read((char*)&students[i], sizeof(Student));
        cout << students[j].id << ' ' << students[j].name << ' ' << students[j].score << endl;
    }
    
    ifs2.close();
    return 0;
}

img

我的问题:
代码虽然可以输出结果,但是最终在运行结束后报错,这个错误应该怎样解决呢?以下为报错图片

img

img


想问一下,read函数与write函数的参数是什么,怎样使用,使用方法是什么,这俩函数还有返回值吗?


#include <iostream>
#include <fstream>
using namespace std;

struct Student{
    int id;
    string name;
    double score;
};

int main(){
    // 将3名学生的学号、姓名和成绩写入二进制文件studentinfo.dat中
    ofstream outfile("studentinfo.dat", ios::binary);
    if (!outfile){
        cout << "文件打开失败!" << endl;
        return 1;
    }
    Student s[3] = {{1001, "Tom", 92.5}, {1002, "Jerry", 89.0}, {1003, "Bob", 78.5}};
    outfile.write((char*)s, sizeof(s));
    outfile.close();

    // 将该文件中的数据读到结构体数组中,并将其输出到屏幕上
    ifstream infile("studentinfo.dat", ios::binary);
    if (!infile){
        cout << "文件打开失败!" << endl;
        return 1;
    }
    Student s1[3];
    infile.read((char*)s1, sizeof(s1));
    for (int i = 0; i < 3; i++)
        cout << "学号:" << s1[i].id << "\t姓名:" << s1[i].name << "\t成绩:" << s1[i].score << endl;
    infile.close();

    return 0;
}

学号:1001 姓名:Tom 成绩:92.5
学号:1002 姓名:Jerry 成绩:89
学号:1003 姓名:Bob 成绩:78.5



#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

struct Student {
    int id;
    char name[20];
    float score;
};

int main()
{
    // 写入数据到二进制文件中
    {
        Student students[3];

        students[0].id = 1001;
        strcpy(students[0].name, "Tom");
        students[0].score = 89.5;

        students[1].id = 1002;
        strcpy(students[1].name, "Alice");
        students[1].score = 95.0;

        students[2].id = 1003;
        strcpy(students[2].name, "Bob");
        students[2].score = 78.0;

        ofstream fout("studentinfo.dat", ios::binary);
        if (!fout) {
            cerr << "Failed to open file" << endl;
            return 1;
        }

        for (int i = 0; i < 3; i++) {
            fout.write(reinterpret_cast<char*>(&students[i]), sizeof(Student));
        }

        fout.close();
    }

    // 从二进制文件中读取数据到结构体数组中,并输出到屏幕上
    {
        Student students[3];

        ifstream fin("studentinfo.dat", ios::binary);
        if (!fin) {
            cerr << "Failed to open file" << endl;
            return 1;
        }

        for (int i = 0; i < 3; i++) {
            fin.read(reinterpret_cast<char*>(&students[i]), sizeof(Student));
        }

        fin.close();

        for (int i = 0; i < 3; i++) {
            cout << "Student No." << i << ":" << endl;
            cout << "    ID: " << students[i].id << endl;
            cout << "    Name: " << students[i].name << endl;
            cout << "    Score: " << students[i].score << endl;
        }
    }

    return 0;
}