应用程序在将对象写入文件时崩溃

时间:2017-11-07 17:34:47

标签: c++ file

我正在研究文件编写的概念。我想到了首先编译工作代码然后从代码中理解。但this链接中的代码不起作用。如果我运行应用程序崩溃。我查看了input.txt文件,我希望它是一个文本文件,但首次执行后的内容是“ØûrMichealÄØûrTerryl€”

可能是什么问题? 编辑:下面是代码:

// C++ program to demonstrate read/write of class
// objects in C++.
#include <iostream>
#include <fstream>
using namespace std;

// Class to define the properties
class Contestant {
public:
    // Instance variables
    string Name;
    int Age, Ratings;

    // Function declaration of input() to input info
    int input();

    // Function declaration of output_highest_rated() to
    // extract info from file Data Base
    int output_highest_rated();
};

// Function definition of input() to input info
int Contestant::input()
{
    // Object to write in file
    ofstream file_obj;

    // Opening file in append mode
    file_obj.open("Input.txt", ios::app);

    // Object of class contestant to input data in file
    Contestant obj;

    // Feeding appropriate data in variables
    string str = "Micheal";
    int age = 18, ratings = 2500;

    // Assigning data into object
    obj.Name = str;
    obj.Age = age;
    obj.Ratings = ratings;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    // Feeding appropriate data in variables
    str = "Terry";
    age = 21;
    ratings = 3200;

    // Assigning data into object
    obj.Name = str;
    obj.Age = age;
    obj.Ratings = ratings;

    // Writing the object's data in file
    file_obj.write((char*)&obj, sizeof(obj));

    return 0;
}

// Function definition of output_highest_rated() to
// extract info from file Data Base
int Contestant::output_highest_rated()
{
    // Object to read from file
    ifstream file_obj;

    // Opening file in input mode
    file_obj.open("Input.txt", ios::in);

    // Object of class contestant to input data in file
    Contestant obj;

    // Reading from file into object "obj"
    file_obj.read((char*)&obj, sizeof(obj));

    // max to store maximum ratings
    int max = 0;

    // Highest_rated stores the name of highest rated contestant
    string Highest_rated;

    // Checking till we have the feed
    while (!file_obj.eof()) {
        // Assigning max ratings
        if (obj.Ratings > max) {
            max = obj.Ratings;
            Highest_rated = obj.Name;
        }

        // Checking further
        file_obj.read((char*)&obj, sizeof(obj));
    }

    // Output is the highest rated contestant
    cout << Highest_rated;
    return 0;
}

// Driver code
int main()
{
    // Creating object of the class
    Contestant object;

    // Inputting the data
    object.input();

    // Extracting the max rated contestant
    object.output_highest_rated();

    return 0;
}

1 个答案:

答案 0 :(得分:4)

代码从根本上被打破了,你应该忽略它。

class Contestant {
public:
    // Instance variables
    string Name;
    int Age, Ratings;

因此,Contestant是一个包含string

的类
Contestant obj;

objContestant

// Writing the object's data in file
file_obj.write((char*)&obj, sizeof(obj));
等等,什么?我们只是写出实现用于obj到文件的内存内容?这毫无意义。

文件是字节流。要将某些内容写入文件,我们必须将其转换为易于理解的字节流。这称为“序列化”。我们不能只接受我们的实现在内存中使用的任何数据来表示某些内容并将其写入文件。如果它包含指向其他内存区域的指针会怎么样?!

理解文件最重要的是它们是字节流,您必须将数据定义为字节流以将其写入文件。这就是文件格式。显然,该代码的作者并不理解。由于这个原因,该页面上解释的“理论”也是错误的。