dimanche 19 juin 2016

Save vector of structures on binary file


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?


Aucun commentaire:

Enregistrer un commentaire