#include <iostream> #include <vector> #include <fstream> #include <string> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ class foo { public: string Name = "Баби"; int Age = 5; void show() { cout << "Name = " << Name << endl; cout << "Age = " << Age << endl; } void write(ostream& os) const { os.write((const char*)& Age, sizeof Age); size_t len = Name.length(); os.write((const char*)& len, sizeof len); os.write(Name.c_str(), len); } void read(istream& in) { in.read((char*)& Age, sizeof Age); size_t len; in.read((char*)& len, sizeof len); Name.resize(len); in.read(&Name[0], len); } }; int main(int argc, char** argv) { setlocale(LC_ALL, "Rus"); foo* obj = new foo(); obj->Age = 5; obj->Name = "Масло"; ofstream file("file.bin", ios::binary | ios::app); if (file.is_open()) { obj->write(file); obj->show(); } file.close(); foo* result = new foo(); ifstream file1("file.bin", ios::binary); if (file1.is_open()) { result->read(file1); file1.close(); result->show(); } file1.close(); return 0; } Incorrectly writes and reads from file. Writes age = 5 and Name = "Масло" , and displays age=0 and Name = "" . What could be the problem?