【问题标题】:C++ saving/loading a vector to a fileC ++将向量保存/加载到文件
【发布时间】:2012-09-04 09:56:16
【问题描述】:

我正在用 C++ 创建一个日历应用程序。

这是我的代码:

class appointment
{
public:
    appointment(string aDate, string aTime, string aType,
    string aLocation, string aComments, bool aIsImportant,
    string aReminderDate, string aReminderTime)
    {
        appDate = aDate;
        appTime = aTime;
        appType = aType;
        appLocation = aLocation;
        appComments = aComments;
        appIsImportant = aIsImportant;
        appReminderDate = aReminderDate;
        appReminderTime = aReminderTime;
    }
    void setDate(string aDate)
    {
        appDate = aDate;
    }
    void setTime(string aTime)
    {
        appTime = aTime;
    }
    void setType(string aType)
    {
        appType = aType;
    }
    void setLocation(string aLocation)
    {
        appLocation = aLocation;
            }
    void setComments(string aComments)
    {
        appComments = aComments;
    }
    void setIsImportant(bool aIsImportant)
    {
        appIsImportant = aIsImportant;
    }
    void setReminderDate(string aReminderDate)
    {
        appReminderDate = aReminderDate;
    }
    void setReminderTime(string aReminderTime)
    {
        appReminderTime = aReminderTime;
    }
    string getDate()
    {
        return appDate;
    }
    string getTime()
    {
        return appTime;
    }
    string getType()
    {
        return appType;
    }
    string getLocation()
    {
        return appLocation;
    }
    string getComments()
    {
        return appComments;
    }
    bool getIsImportant()
    {
        return appIsImportant;
    }
    string getReminderDate()
    {
        return appReminderDate;
    }
    string getReminderTime()
    {
        return appReminderTime;
    }
private:
    appointment();
    string appDate;
    string appTime;
    string appType;
    string appLocation;
    string appComments;
    bool appIsImportant;
    string appReminderDate;
    string appReminderTime;
    //person owner;
};

class calendar
{
public:
    calendar()
    {
        loadFromFile();
    }
    ~calendar()
    {
        saveToFile();
    }
    void createAppointment(string aDate, string aTime, string aType,
    string aLocation, string aComments, bool aIsImportant,
    string aReminderDate, string aReminderTime)
    {
        appointment newAppointment(string aDate, string aTime, string aType,
        string aLocation, string aComments, bool aIsImportant,
        string aReminderDate, string aReminderTime);
        //appointments.resize(appointments.size() + 1,newAppointment);
    }
private:
    vector<appointment> appointments;
    string calCurrentDate;
    string calCurrentTime;
    void loadFromFile()
    {
        //Code to load appointments from file
    }
    void saveToFile()
    {
        //Code to save appointments to file
    }
};

我可以在以下方面提供一些帮助吗:

当调用构造函数时,我想加载约会对象的文件(loadFromFile() 方法)并将“约会”变量设置为包含该文件的内容。在 saveToFile() 方法之后,我想将约会向量的内容保存到文件中。

另外,当调用 createAppointment() 方法时,我想将向量的大小增加 1,并将内容添加到向量中。我不确定代码是否正确。

从文件更新保存/加载

    void loadFromFile()
    {
        ifstream iStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        iStream.read((char*)&fHeader, sizeof(fileHeader_t));
        if (fHeader.magicNumber = 0xDEADBEAF)
        {
            appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
        }
    }
    void saveToFile()
    {
        ofstream oStream("file.ext", ios::binary);
        fileHeader_t fHeader;
        fHeader.magicNumber = 0xDEADBEAF;
        fHeader.appointmentCount = appointments.size();
        oStream.write((char*)&fHeader, sizeof(fileHeader_t));
        oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
    }

【问题讨论】:

标签: c++ vector save


【解决方案1】:

向量应该是连续的,所以使用 ifstream 加载它们应该没有任何问题,如果我是你,我会为你的二进制文件创建一个基本的头文件,例如:

struct fileHeader_s
{
    DWORD magicNumber;
    size_t appointmentsCount;
}fileHeader_t;

然后,在一个循环中,只需读取约会值中的每个项目,并使用约会.push_back(item); 你应该在 createAppointment 中做同样的事情,不要调整向量的大小,只需执行 push_back(newAppointment);

【讨论】:

  • 虽然目前没有不连续存储它的实现,但它实际上并没有在规范中定义,因此不能依赖它。此外,使用 ifstream 保存需要序列化相关类和序列化向量,标准不支持。
  • @slugonamission 如果我们谈论的是std::vector,那么是的,它保证按标准连续存储其元素。
  • @slugonamission 因为我们在谈论std::vector:23.3.6.1:1(类模板向量概述)状态:The elements of a vector are stored contiguously, meaning that if v is a vector&lt;T, Allocator&gt; where T is some type other than bool, then it obeys the identity &amp;v[n] == &amp;v[0] + n for all 0 &lt;= n &lt; v.size().。所以它定义了一个向量必须连续存储元素。
  • 大多数(如果不是全部)实现都使用连续向量,因为它由 STL 指定:link“就像常规数组一样,向量容器的元素存储在连续的存储位置”关于序列化,首先他不会使用用于输入的ifstream,而是使用ofstream,他可以:fStream.write((char*)&vector[0], sizeof(appointment) * vector.size()) ;
  • 对于约会.push_back(newAppointment),我收到以下错误:“ E2064 无法使用 'appointment(string,string,string,string,string,bool,string ,string)' E2342 参数 '_Val' 中的类型不匹配(需要 'const 约会 &',得到 '约会 (*)(string,string,string,string,string,bool,string,string)')"
【解决方案2】:

您将不得不提出自己的文件格式(即手动将每条记录保存在一行上),或使用类似Boost::serialization 的格式。

【讨论】:

  • 您认为最简单的方法是什么?
  • 在这种情况下,boost::serialization,因为您使用的是类向量,否则您必须实现自定义方法来序列化/反序列化您的类。
猜你喜欢
  • 2013-02-22
  • 1970-01-01
  • 2012-09-21
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 2011-11-06
相关资源
最近更新 更多