#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?

  • @AnT I still can not solve the problem. Changed a little code, the problem remains. - justlead

1 answer 1

File do not forget to close. Before trying to open a file for reading, you should not forget to close the previous opening (for writing). And do not forget to check if you opened the file successfully - then you yourself would understand why reading does not work.

You did not write / read as effectively as it could, but nevertheless everything is correct.


More effective:

  1. If you write the length of a string, then it is not necessary to write its zero symbol with the string.

  2. When reading a string, you can carefully read it directly inside std::string , without creating an intermediate buffer

for example

 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.data(), len); // или `in.read(&Name[0], len);` } 
  • Thank! But my line is in.read(Name.data(), len); Writes Аргумент типа const char* не совместим с типом char* I understand that this is due to the fact that Name is here const char * and how to translate it into char *? - justlead
  • @justlead: Not Name , but Name.data() . Which compiler are you using? Here you need C ++ 17. If you get this error, replace this line with in.read(&Name[0], len); - AnT
  • Writing to the file variables of type size_t and int will lead to fun bugs when changing the bit. It would be necessary to convert to types with a fixed bit width. - gbg
  • @gbg: Features of the world of binary files. Any binary file is developed for a specific set of requirements for lifetime / portability: within the execution session, between execution sessions, between program versions, between programs of this C ++ platform, between platforms, etc. Each option has its own set of requirements. - AnT
  • Well, no, this is a manifestation of short-sightedness. It was enough to use a fixed format to get a portable program. And so we got potential and very expensive problems. - gbg