1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| void initialize(std::map<std::string TypeProduit> & lamap)
{
/* ouvrons le fichier contenant les produits */
std::ifstream ifs("fichier.txt");
/* il nous faut des variables temporaires pour les différentes données
* à lire
*/
std::string nom;
std::string info;
std::string etagère;
double prix;
int stock;
int alerte;
/* le nom tient sur une ligne complete
* nous retournons dans la boucle tant qu'on trouve un nom
*/
while(std::getline(ifs,nom))
{
/* l'info tient aussi sur une ligne complète*/
std::getline(ifs,info);
/* les trois dernières infos tiennent sur une seule ligne */
ifs>>prix>>stock>>alerte;
/* créons la fiche produit sur base de ce qu'on a lu, et
* plaçons la dans la map
*/
FicheProduit p(nom,info,prix,stock,alerte);
lamap.insert(std::make_pair(nom,p));
}
/* fini :D */
}
void finalize(std::map<std::string TypeProduit> & lamap)
{
/* ouvrons le fichier en écrasement */
std::ofstream ofs("fichier.txt");
/* tant que l'on trouve une fiche produit, nous l'écrivons dans le fichier
*/
for(std::map<std::string TypeProduit>::const_iterator it=lamap.begin();
it!=lamap.end();++it)
(*it).second.write(ofs);
/*fini :D */
} |