gpt4 book ai didi

C++将 vector 保存/加载到文件

转载 作者:行者123 更新时间:2023-11-30 02:03:08 25 4
gpt4 key购买 nike

我正在用 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() 方法之后,我想将约会 vector 的内容保存到文件中。

此外,当调用 createAppointment() 方法时,我想将 vector 的大小增加 1,并将内容添加到 vector 中。我不确定正确的代码。

从文件更新保存/加载

    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());
}

最佳答案

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

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

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

关于C++将 vector 保存/加载到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12370229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com