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

时间:2012-09-11 12:55:29

标签: c++ vector save

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

这是我的代码:

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()方法)并设置'appointmentments'变量以包含该文件的内容。在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());
    }

2 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:0)

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