Recently I got an annoying bug about read data into a vector of A Class.I have written a simple program to test the fstream in main function and it worked without any problem. But while I coding a Book Class in Book.h and implement the Class in Book.cpp,then attempted to write vector of Book into a file with ofstream,no bug occurred.
//write data
ofstream output("book.bat",ios::binary);
if( !output ){
exit(-1);
}
output.write(reinterpret_cast<char*>(&books[0]),sizeof(Book)*books.size());
output.close();
//read data
ifstream input("book.bat",ios::binary);
if( !input ){
exit(-1);
}
Book tmp;
while( input.read(reinterpret_cast<char*>(&tmp),sizeof(Book)) ) {
books.push_back(tmp);
}
input.close();
Unfortunately when program run to the step of readding file into vector,the bug occurred.Having searched for some hours, I got nothing to deal with the bug. Thanks for help.