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!