0

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.

windy Yu
  • 1
  • 1
  • 1
    Why not describe the bug you see? Also please do not forget to post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Jul 25 '16 at 15:38
  • You should look at [serialization](http://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c) – NathanOliver Jul 25 '16 at 15:39
  • 1
    Writing an empty vector is not going to work well for you since you access the first element. My crystal ball also says `Book` has a `std::string` member, and reading/writing the binary representation of any pointers within isn't going to work since that pointer will be invalid on next run. – chris Jul 25 '16 at 15:40
  • Note that you can write/read only objects of the POD type (http://en.cppreference.com/w/cpp/concept/PODType) as binary dump – AnatolyS Jul 25 '16 at 15:42
  • probably not the point (the POD stuff is the answer) but your example cannot work as is since book variable is not empty when read, you just dumped it. – Jean-François Fabre Jul 25 '16 at 15:44
  • `output.write(reinterpret_cast(&books[0]),sizeof(Book)*books.size());` -- `input.read(reinterpret_cast(&tmp),sizeof(Book)` -- I see stuff like this over and over again for the same issue. This code will *not* work for non-POD types, but for some reason, there are tons of posts on SO that do this same mistake. I wonder if it is coming from a book or a website that is being used by a lot of new C++ programmers. – PaulMcKenzie Jul 25 '16 at 15:50
  • First of all, MikeCAT's suggestion,learned a lot from M,C, a V example.And as for chris,you are amazing! Book does has a std::string,but the simple program tested in the main function could run well while according to the defination of POD,It is correct that std::string leads to bug. Thanks for you guys. – windy Yu Jul 26 '16 at 07:28

1 Answers1

0

For a point of view of class as a type that encapsulates the internal representation, to read/write using the direction of memory and size of the type is an error. This could work for POD types, but if book is a type with some data as pointers to dynamic memory, your code will not work.

EFenix
  • 831
  • 4
  • 11