C++将矢量保存/加载到文件中

C++ saving/loading a vector to a file

本文关键字:加载 文件 保存 C++      更新时间:2023-10-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());
    }

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

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

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

您将不得不想出自己的文件格式(即手动将每个记录保存在一行),或者使用类似Boost::序列化的方法。