0

I have

int main()
{
for(;;)
{
Database database;
in.read((char*)&database, 10*sizeof(database));
in.close();

if (database.MAIN.created == true) {
out.open("database.txt", ios::app);
out.write((char*)&database, 10 * sizeof(database)); out.close();
//maipulation.......}

else(){database.create_main()}
}
}

class Database
{
public:

   Database();
   void create_main();


   Main MAIN;
   std::vector<secondmain> second_main;
   std::vector<thirdmain> third_main;
   std::vector<clas> obj;
}

When I run the program it only saves the MAIN part even if I am creating vectors of the object in the same instance of the program. I can view those vectors and edit them but when I restart the program only the main object exists and the program crashes when it gets to a line where it has to deal with the other objects. Could it be because the vectors are not being saved properly?

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
MROJ4N
  • 1
  • Can you copy a text of error? – NixoN Jun 05 '20 at 05:04
  • A number of questions arise here, but I would start with what do you expect `read` and `write` to do when you tell them that a Database object is just an array of characters? – jkb Jun 05 '20 at 05:09
  • 2
    Does this answer your question? [Writing/reading large vectors of data to binary file in c++](https://stackoverflow.com/questions/28886899/writing-reading-large-vectors-of-data-to-binary-file-in-c) (Sorry, this proposed duplicate rambles a bit and fails to have a nicely minimized example. Your question is better in this regard, but the other question already has an answer applicable to yours.) – JaMiT Jun 05 '20 at 05:13
  • I am basically trying to have database saved so it can be used again to add and view data. There is no error, the program just stops and returns -1073741819. – MROJ4N Jun 05 '20 at 05:25
  • @jamit that example says to enter the each object of vector separately through a loop. i could do that but i thought it could be possible to just copy the entire object into the file and read it when the program starts without any loops. Is that not possible? – MROJ4N Jun 05 '20 at 05:28
  • @MROJ4N No it's not possible; `vector`s contain pointers to their data, not the data itself. If the objects are not trivially copyable you need to serialize them, – molbdnilo Jun 05 '20 at 06:07
  • @MROJ4N If you write more than a few bytes of data, there is a loop somewhere. You might not see it, but it's there. If you don't want to see the loop in your `main` function, you could implement `std::ostream & operator<<(std::ostream & os, const Database & db)` and `std::istream & operator>>(std::istream & is, Database & db)`. There is also a [non-portable approach for writing vectors](https://stackoverflow.com/questions/2469531/reading-and-writing-c-vector-to-a-file) that pushes the loop into the standard library, assuming the objects in the vectors are simple enough. – JaMiT Jun 05 '20 at 06:07
  • Reading and writing ten times the size of a `Database` when there's only one `Database` has undefined behaviour. – molbdnilo Jun 05 '20 at 06:08
  • @molbdnilo ah yeah forgot to remove that when I copy pasted. It was a potential fix I thought might work... It is not there – MROJ4N Jun 05 '20 at 15:32

0 Answers0