Программы на C++. Форум разработчиков
-
Anonymous
Сохранение объекта класса в двоичный файл
Сообщение
Anonymous »
Я ищу простой способ сохранить и загрузить этот объект C++ в двоичный файл и обратно.
Код: Выделить всё
#include
#include
class User
{
private:
std::string _name;
int _age;
std::vector _hobbies;
public:
std::string Name() { return _name; }
int Age() { return _age; }
std::vector Hobbies() { return _hobbies; }
void Hobbies(std::string hobbieName)
{
_hobbies.push_back(hobbieName);
}
User(std::string name, int age)
{
_name = name;
_age = age;
}
};
int main()
{
User u1 = User("John", 48);
u1.Hobbies("Art");
u1.Hobbies("Lego");
u1.Hobbies("Blogging");
User u2 = User("Jane", 37);
u2.Hobbies("Walking");
u2.Hobbies("Chess");
u2.Hobbies("Card Games");
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/67035898/saving-class-object-to-binary-file[/url]
1736640142
Anonymous
Я ищу простой способ сохранить и загрузить этот объект C++ в двоичный файл и обратно.
[code]#include
#include
class User
{
private:
std::string _name;
int _age;
std::vector _hobbies;
public:
std::string Name() { return _name; }
int Age() { return _age; }
std::vector Hobbies() { return _hobbies; }
void Hobbies(std::string hobbieName)
{
_hobbies.push_back(hobbieName);
}
User(std::string name, int age)
{
_name = name;
_age = age;
}
};
int main()
{
User u1 = User("John", 48);
u1.Hobbies("Art");
u1.Hobbies("Lego");
u1.Hobbies("Blogging");
User u2 = User("Jane", 37);
u2.Hobbies("Walking");
u2.Hobbies("Chess");
u2.Hobbies("Card Games");
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/67035898/saving-class-object-to-binary-file[/url]