2012-09-11 30 views
1

我正在用C++創建日曆應用程序。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()); 
    } 
+0

非常類似於例如這個問題:http://stackoverflow.com/questions/5110306/loading-data-into-a-vector-of-structures – codeling

回答

0

加載你將不得不拿出自己的文件格式(即手動保存一行每個記錄),或使用類似Boost::serialization

+0

在你看來,最簡單的是什麼? –

+0

在這種情況下,boost :: serialization,因爲你正在使用一個類的向量,否則你必須實現一個自定義的方式來序列化/反序列化你的類。 – slugonamission

4

載體是所謂連續的,所以你不應該對使用ifstream的裝載他們的任何問題,如果我是你,我會創建一個基本報頭爲你的二進制文件的東西,如:

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

然後,在循環中,只需讀取預約值中的每個項目,並使用appointmentments.push_back(item); 你應該在createAppointment中做同樣的事情,不要調整vector的大小,只要push_back(newAppointment);

+0

雖然目前還沒有實現並不存儲它,但它並沒有在規範中實際定義,所以它不能被依賴。此外,使用ifstream進行保存需要序列化有問題的類和序列化向量,這是標準不支持的。 – slugonamission

+0

@slugonamission如果我們談論的是'std :: vector',那麼是的,它保證通過標準**連續地存儲它的元素**。 –

+2

@slugonamission因爲我們在談論'std :: vector':23.3.6.1:1(類模板向量概述)指出:' 向量的元素是連續存儲的,這意味着如果v是一個向量其中T是某種類型的其他類型 比bool,那麼它對所有0 <= n