我正在用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());
}
非常類似於例如這個問題:http://stackoverflow.com/questions/5110306/loading-data-into-a-vector-of-structures – codeling