En fait je pense que c'est une mauvaise approche (enfin c'est moins point de vue). Globalement l'accès à un fichier est lent, donc il vaut mieux charger le fichier dans une structure de donnée, manipuler cette structure, et réécrire le fichier si nécessaire.
#include <map>
#include <iostream>
#include <fstream>
#include <set>
#include <string>
typedef std::string matricule_t;
typedef std::string nom_t;
typedef std::string prenom_t;
class datas_etudiant_t{
protected:
typedef std::map<matricule_t,std::pair<nom_t,prenom_t> > map_matricule_t;
typedef std::map<nom_t,std::set<matricule_t> > map_nom_t;
typedef std::map<prenom_t,std::set<matricule_t> > map_prenom_t;
map_matricule_t map_matricule;
map_nom_t map_nom;
map_prenom_t map_prenom;
public:
// Le constructeur
datas_etudiant_t(){}
// Ajouter un étudiant
void ajouter_etudiant(
const matricule_t & matricule,
const nom_t & nom,
const prenom_t & prenom
){
map_matricule[matricule] = std::make_pair(nom,prenom);
map_nom[nom].insert(matricule);
map_prenom[prenom].insert(matricule);
}
// Recherche sur le matricule
bool trouver_matricule(
const matricule_t & matricule,
nom_t & nom,
prenom_t & prenom
){
map_matricule_t::const_iterator fit(map_matricule.find(matricule));
if (fit == map_matricule.end()) return false;
nom = (fit->second).first;
prenom = (fit->second).second;
return true;
}
// Recherche sur le prenom
bool trouver_prenom(
const prenom_t & prenom,
std::set<matricule_t> & matricules
){
map_prenom_t::const_iterator fit(map_prenom.find(prenom));
if(fit == map_prenom.end()) return false;
matricules = fit->second;
return true;
}
// Recherche sur le nom
bool trouver_nom(
const nom_t & nom,
std::set<matricule_t> & matricules
){
map_nom_t::const_iterator fit(map_nom.find(nom));
if(fit == map_nom.end()) return false;
matricules = fit->second;
return true;
}
template <typename Tstream>
void write(Tstream & out) const{
map_matricule_t::const_iterator mit(map_matricule.begin()),mend(map_matricule.end());
for(;mit!=mend;++mit){
const matricule_t & matricule = mit->first;
const nom_t & nom = (mit->second).first;
const prenom_t & prenom = (mit->second).second;
out << matricule << '\t' << nom << '\t' << prenom << std::endl;
}
}
};
int main(){
datas_etudiant_t datas;
datas.ajouter_etudiant("AP123","Poulain","Amélie");
datas.ajouter_etudiant("AP124","Mauresmo","Amélie");
datas.ajouter_etudiant("SP69","Poulain","Super");
datas.ajouter_etudiant("PP99","Parker","Peter");
{
const std::string prenom_cherche = "Amélie";
std::cout << "rechercher " << prenom_cherche << std::endl;
std::set<matricule_t> matricules;
if(datas.trouver_prenom(prenom_cherche,matricules)){
std::set<matricule_t>::const_iterator sit(matricules.begin()),send(matricules.end());
for(;sit!=send;++sit) std::cout << *sit << std::endl;
}else std::cout << "Pas de personne dont le prénom est " << prenom_cherche << std::endl;
}
{
const std::string nom_cherche = "Poulain";
std::cout << "rechercher " << nom_cherche << std::endl;
std::set<matricule_t> matricules;
if(datas.trouver_nom(nom_cherche,matricules)){
std::set<matricule_t>::const_iterator sit(matricules.begin()),send(matricules.end());
for(;sit!=send;++sit) std::cout << *sit << std::endl;
}else std::cout << "Pas de personne dont le nom est " << nom_cherche << std::endl;
}
// Ecrire dans la console
datas.write(std::cout);
// Ecrire dans un fichier
std::ofstream ofs("plop.txt");
if(ofs){
datas.write(ofs);
ofs.close();
}else std::cerr << "fichier invalide" << std::endl;
return 0;
}
Si tu veux faire une fonction de modification en fonction du matricule, pense à corriger en conséquence les 3 maps de datas_etudiant_t. En toute rigueur la fonction ajouter_etudiant doit vérifier que personne n'a déjà cet identifiant.
Bonne chance