I try to write class instances to a file through ofstream, then read through ifstream. Problem:

  • Recorded a copy of 1 in a binary file, saved. Then the second and so on. I read the data from the file - everything works as it should.
  • He closed, restarted the program (console), chose the item "Output all records" - DOES NOT work ... Or krakozyabry on the screen, or crash the program ... 3 hours looking for a problem, as far as I understand the problem is that it is BAD to save the class to a file , you need to do serialization, there is no time for it ... I will be grateful if anyone can help in solving the problem.

Video (there is debugging information, do not pay attention to it, please) - http://youtu.be/oZ-MvsdW0qA?hd=1

CLASS:

class UserList{ char * _listName; public: UserList(); UserList(int); void SetListName(char *); void temp(); // @DEB void read(ifstream *in) { in->read((char *) this, sizeof(UserList)); } void write(ofstream *out){ out->write((char *) this, sizeof(UserList)); } }; 

RECORDING TO CLASS FILE FILE

 void WriteToDB(UserList data, bool overwrite) { ofstream out("AppData/DB_lists", ofstream::app); data.write(&out); out.flush(); out.close(); cout << "\n\nWRITTEN to DB\n"; _getch(); return; } 

READING A FILE

 void ReadFromDB(char * pathtData, int counter) { UserList ** datas = new UserList *[counter]; for (int i = 0; i < counter; i++){ datas[i] = new UserList(1); } ifstream in("AppData/DB_lists"); for (int i = 0; i < counter; i++){ datas[i]->read(&in); } in.close(); for (int i = 0; i < counter; i++){ datas[i]->temp(); // Temp - вывести название списка. } return; } 

TEMP METHOD

 void UserList::temp() { cout << "\nName is: " << _listName; return; } 

I would be grateful for the help!

  • one
    It is better to write / read to the file in your case in binary mode (add the std :: ios :: binary flag to the ofstream / ifstream constructors). - Vladimir Gamalyan

2 answers 2

Disclaimer: Use something higher for serialization / deserialization.

The most likely reason is that you save the char * UserList :: _ listName pointer to the file instead of the data it points to. As long as you started writing / reading one after another, the data probably stayed in the same places, so it looked as if everything was working fine. And after the restart, of course there was already garbage.

  • That's right, the reason was in char * UserList :: _ listName. Replaced with char * _listname [64], and it all worked. Thank you very much, @Vladimir Gamalian! - Deco Yo
  • @DecoYo I hope that still on char _listname[64] . Just remember - if you add at least one virtual function, you will get in trouble. It would be better for you not to use memory recording from the address this - well, this is an unhealthy occupation! - Harry
  • Yes, I was mistaken, I replaced it with char _listname [64]. Thank you, I went to study materiel) - Deco Yo

I hope that SetListName dynamically allocates memory for _listName , while not forgetting to release the previous one, etc.? Then I would do this (in fact, I still need to check for a null pointer; I think you’ll add it yourself):

 void read(ifstream *in) { size_t sz; in->read(&sz, sizeof(sz)); char * buf = new char[sz]; in->read(buf, sz); SetListName(buf); delete[] buf; } void write(ofstream *out){ size_t sz = strlen(_listName)+1; out->write(&sz, sizeof(sz)); out->write(_listName,sz); } 

And, of course, work with the file in binary mode!

For the future - never write just this . Even the presence of a single virtual function, not to mention the pointers, makes it unworkable ...