首页 > 解决方案 > 为什么 fstream 的读写功能会在我的程序中的两个不同点导致错误?

问题描述

在我的计算机科学课上,我们使用 fstream 来读取和写入文件。这是我们以前做过的事情,但是我们需要使用 fstream 的读写功能。当我在 SaveToBinary 中使用 write 函数并在 LoadFromBinary 中使用 read 函数时,它会导致在第 144 行或第 53 行发生错误。错误显示在 xmemory 中的第 1222 行。我不知道错误是什么,但我相信是写入和读取功能导致它,因为使用 >> 和 << 访问文件工作正常。

这是我的代码。有一些测试代码被注释掉或输出到控制台。

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

struct Student {
    string mName = "Name"; //Switch to char*
    int mAge = 0;
    double mGrades[4] = { 0, 0, 0, 0 };
    double mAverage = 0;
};

//Prototypes
vector<Student> LoadFromText(string);
string SaveToBinary(vector<Student>&);
vector<Student> LoadFromBinary(string);

void display(Student student) {
    cout << "Name: " << student.mName << endl;
    cout << "Age: " << student.mAge << endl;

    cout << "Grades: ";
    for (int j = 0; j < 4; j++) {
        cout << student.mGrades[j] << " ";
    }
    cout << endl;

    cout << "Average: " << student.mAverage << endl;
    cout << endl;
}


int main() {
    vector<Student> students = LoadFromText("students.txt");
    string fileName = SaveToBinary(students);
    vector<Student> studentsFromBinary = LoadFromBinary(fileName);

    cout << "Main Method" << endl;

    for (int i = 0; i < studentsFromBinary.size(); i++) {
        display(studentsFromBinary[i]);
    }

    cout << "Printed" << endl;
    return 0; //ERROR FLAGS HERE
}

//THIS METHOD READS DATA FROM "student.txt" INTO A vector<student> AND RETURNS
vector<Student> LoadFromText(string aFileName) {
    fstream file;
    file.open(aFileName, ios::in);
    vector<Student> students;

    if (file.is_open()) {
        Student temp;

        string name, nameLast;
        const int amountOfGrades = 4;
        double total = 0;

        cout << "file opened" << endl;

        while (file >> name) {
        //Save Name
            //Convert string to char*
            file >> nameLast;
            name = name + " " + nameLast;
            
            temp.mName = name;

        //Save Age
            file >> temp.mAge;

        //Save Grades and average
            for (int i = 0; i < amountOfGrades; i++) {
                file >> temp.mGrades[i];
                total += temp.mGrades[i];
            }

            temp.mAverage = total / amountOfGrades;
            total = 0;

            //display(temp);

            students.push_back(temp);
        }
    }
    else
        cout << "\n\tError: " << aFileName << " not found" << endl;

    file.close();

    return students;
}

//THIS METHOD READS DATA FROM vector<student> INTO "student.dat" AND RETURNS "student.dat"
string SaveToBinary(vector<Student>& aStudents) {
    fstream file;
    file.open("students.dat", ios::out | ios::binary);

    if (file.is_open()) {
        for (int i = 0; i < aStudents.size(); i++){
            Student student = aStudents[i];
            file.write(reinterpret_cast<char*>(&aStudents[i]), sizeof(aStudents[i]));

            /*file << student.mName << endl;
            file << student.mAge << endl;
            for (int i = 0; i < 4; i++) {
                file << student.mGrades[i] << endl;
            }
            file << student.mAverage << endl;*/
        }
    }
    else
        cout << "\n\tError: " << "students.dat" << " not found" << endl;

    file.close();

    return "students.dat";
}

//THIS METHOD READS DATA FROM "student.DAT" INTO A vector<student> AND RETURNS
vector<Student> LoadFromBinary(string aFileName) {
    vector<Student> students;
    fstream file;
    file.open(aFileName, ios::in | ios::binary);

    if (file.is_open()) {
        Student temp;

        while (!file.eof()) {
            file.read(reinterpret_cast<char*>(&temp), sizeof(temp));

        /*  string last;
            file >> temp.mName >> last;
            temp.mName += " " + last;
            file >> temp.mAge;
            for (int i = 0; i < 4; i++) {
                file >> temp.mGrades[i];
            }
            file >> temp.mAverage;*/

            students.push_back(temp);
            cout << students.size() << endl;

            //Output
                cout << temp.mName << endl;
                cout << temp.mAge << endl;
                for (int i = 0; i < 4; i++) {
                    cout << temp.mGrades[i] << endl;
                }
                cout << temp.mAverage << endl;

                cout << endl;
        }

        students.resize(students.size() - 1);
    } //ERROR FLAGS HERE
    else
        cout << "\n\tError: " << "students.dat" << " not found" << endl;
    
    file.close();

    return students;
}

这是我们正在使用的文本文件。

Augustina Aceto
23
62.25 100 40.50 100


Louann Levron
21
40.80 75.30 52.75 64.50


Jaimie Jumper
23
99.0 0.0 40.60 75.50


Lanie Labelle
22
94.0 100.0 90.30 100.0


Phillip Pless
24
100.0 0.2 40.1 75.7

标签: c++fstream

解决方案


推荐阅读