1

我正在用 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());
    }
4

2 回答 2

4

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

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

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

于 2012-09-11T13:06:15.567 回答
0

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

于 2012-09-11T12:56:46.503 回答