0

I need to save a vector of structs on a binary file, and get this data back when the program starts again.

struct produc{
   string nanme;
   float price;
   int id;
   int idp;
};

vector <produc> products;

scanf("%s",des.c_str());
scanf("%d",&cod);
scanf("%d",&prov);
scanf("%f",&price);


produc pr;
pr.name = des.c_str();
pr.price=precio;
pr.id=cod; 
pr.idp=prov;  

products.push_back(pr);

This is how I ask for new data and put on a vector, then I just use a for to show the info.

int l = products.size();
produc pr;
for(int i=0;i<l;i++)
{
    pr = productos[i];
    cout << "\t Code        : " << pr.id     << endl;
    cout << "\t Product     : " << pr.nombre << endl;
    cout << "\t Prov Code.  : " << pr.idp    << endl;
    cout << "\t Priceo      : " << pr.precio << endl << endl;
}

I would like to save this vector on a binary file, if i close the program recover it and put it on a vector. I was following the answer on this post but I struggling and not sure how implement it How to read / write a struct in Binary Files?

Edited.

I actually try to write/read individually my struct, but I was wondering if was an option to save the whole vector. Either way how I do it doest work cuz when I try to read back I got an Error: Segmentation fault (core dumped) This is how I write:

ofstream fichero("produts.data", ios::app | ios::binary);
if(!fichero) {
    cout << "Can't open";
}
fichero.write((char *) &pr, sizeof(produc));
fichero.close();

read:

ifstream fichero("produts.data", ios::in | ios::binary);
if(!fichero){
    cout << "Can't open";
    exit(0);        
}
fichero.read((char *)&pr,sizeof(produc));
fichero.close();
Community
  • 1
  • 1
Ellebkey
  • 2,201
  • 3
  • 22
  • 31
  • So which answer did you use at the link you posted? Hopefully it was **not** the accepted answer, as that code is terribly broken. – PaulMcKenzie Jun 18 '16 at 23:34
  • Both, and like you say the first one has a lot of bugs, and I didn't understand quite well the second one – Ellebkey Jun 18 '16 at 23:40
  • `scanf("%s",des.c_str());` -- This is undefined behavior. The `c_str()` function, assuming that `des` is a `std::string`, returns a `const char *`. Thus you cannot write into that buffer. – PaulMcKenzie Jun 18 '16 at 23:40
  • 1
    The accepted answer has bugs in the sense that it attempts to write a non-POD struct to a file as if it is a POD type. You cannot write non-POD structs to a file using stuff like `write((char*)(produc), sizeof(produc));` This [small program](http://ideone.com/kv8xkX) that calls `is_pod` shows that your `produc` is not a POD type, so forget that first answer altogether. The struct is non-POD due to the `std::string` member. – PaulMcKenzie Jun 18 '16 at 23:46
  • You need to write the **data** from each member of your struct to the file, and not the entire struct in one big gulp (like the first answer at the link was trying to do). For example, the `std::string`, you write the data that the `std::string` represents, i.e. the character string. On reading, you read each item separately, and from those separate items, **construct** a `produc` instance with that data. Note the word **construct**. Also, please post the attempt of reading / writing to the file. I think you're being downvoted for this missing info. – PaulMcKenzie Jun 18 '16 at 23:52
  • I edited my question, hope you can help me @PaulMcKenzie – Ellebkey Jun 19 '16 at 00:36
  • Well, as I stated, your code will not work correctly. If you want proof it doesn't work, the `sizeof` is a compile-time value, in other words, it never changes value. Even if the string contained 1,000 characters, `sizeof(produc)` is the same value. Assume `sizeof(produc)` is 16. You're writing 16 bytes to the file, no matter how many characters are in the string. Also, you don't construct objects by reading values into it. You need to **construct** the object and set its members the old-fashioned way. There are plenty of links on SO explaining "serialization of objects". – PaulMcKenzie Jun 19 '16 at 00:54

0 Answers0