Fichier en C++
Résolu
freddo
-
swed -
swed -
Bonjour,
je suis entrain de faire un traitement dans un fichier en C++ donc je voudrai supprimer une ligne selon un choix bien précis
j'ai écrit une fonction rechercher_element() qui fonctionne parfaitement mais mon problème se pose au niveau de la suppression dans le fichier
alors si quelqu'un pourai m'aider ça serrai cool.
void gestionEmployers::retirerEmployer(long mat)
{
int m=gestionEmployers::rechercherEmployer(mat);
ifstream f("C:/Employer.text",ios::in);
while(f.good())
{
long a;
string b,c,d;
int e;
double t;
f>>a>>b>>c>>d>>e>>t;
Employer E(a,b,c,d,e,t);
tab3.push_back(&E);
}
tab3.erase(tab3.begin()+m);
ofstream F("C:/Employer.text",ios::out|ios::trunc);
for(unsigned int i=0;i<tab3.size();i++)
F<<tab3[i]->getMatricule()<<" "<<tab3[i]->getcaract()<<" "<<tab3[i]->getNom()<<" "<<tab3[i]->getPrenom()<<" "<<tab3[i]->getAge()<<" "<<tab3[i]->getSalaire()<<endl;
F.close();
}
int gestionEmployers::rechercherEmployer(long mat)
{
fstream f("C:/Employer.text",ios::in|ios::out);
long a;
string b,c,d;
int e;
double t;
tab3.clear();
int i=0;
while(f.good())
{
f>>a>>b>>c>>d>>e>>t;
Employer E(a,b,c,d,e,t);
tab3.push_back(&E);
if(tab3[i]->getMatricule()==mat)
{
f.close();
return i;
}
i++;
}
f.close();
return -1;
}
je suis entrain de faire un traitement dans un fichier en C++ donc je voudrai supprimer une ligne selon un choix bien précis
j'ai écrit une fonction rechercher_element() qui fonctionne parfaitement mais mon problème se pose au niveau de la suppression dans le fichier
alors si quelqu'un pourai m'aider ça serrai cool.
void gestionEmployers::retirerEmployer(long mat)
{
int m=gestionEmployers::rechercherEmployer(mat);
ifstream f("C:/Employer.text",ios::in);
while(f.good())
{
long a;
string b,c,d;
int e;
double t;
f>>a>>b>>c>>d>>e>>t;
Employer E(a,b,c,d,e,t);
tab3.push_back(&E);
}
tab3.erase(tab3.begin()+m);
ofstream F("C:/Employer.text",ios::out|ios::trunc);
for(unsigned int i=0;i<tab3.size();i++)
F<<tab3[i]->getMatricule()<<" "<<tab3[i]->getcaract()<<" "<<tab3[i]->getNom()<<" "<<tab3[i]->getPrenom()<<" "<<tab3[i]->getAge()<<" "<<tab3[i]->getSalaire()<<endl;
F.close();
}
int gestionEmployers::rechercherEmployer(long mat)
{
fstream f("C:/Employer.text",ios::in|ios::out);
long a;
string b,c,d;
int e;
double t;
tab3.clear();
int i=0;
while(f.good())
{
f>>a>>b>>c>>d>>e>>t;
Employer E(a,b,c,d,e,t);
tab3.push_back(&E);
if(tab3[i]->getMatricule()==mat)
{
f.close();
return i;
}
i++;
}
f.close();
return -1;
}
A voir également:
- Fichier en C++
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
1 réponse
Bonjour,
Sans lire ton code :
Pour faire simple, j'aurais utilisé deux fichier, Fentrée et Fsortie, avec cet algo :
Pour chaque ligne de Fentrée
-- Si ce n'est pas la ligne à supprimer, écriture dans le fichier Fsortie
Prochaine ligne de Fentrée
Au pire, suivi d'un déplacement de FSortie vers FEntrée...
Cordialement,
M.
Sans lire ton code :
Pour faire simple, j'aurais utilisé deux fichier, Fentrée et Fsortie, avec cet algo :
Pour chaque ligne de Fentrée
-- Si ce n'est pas la ligne à supprimer, écriture dans le fichier Fsortie
Prochaine ligne de Fentrée
Au pire, suivi d'un déplacement de FSortie vers FEntrée...
Cordialement,
M.