Bonsoir a tous,
j'ai écrit cette classe qui rempli un fichier txt par des objets de type matiere
matiere.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include<iostream>
#include<string>
#include <fstream>
 
using namespace std;
class matiere
{
	string m_nom;
	int m_cof;
public:
	matiere():m_nom(" "),m_cof(0){} ;
	matiere(string nom,int cof):m_nom(nom),m_cof(cof){};
	friend istream &operator>>(istream &,matiere &);
	friend ostream &operator<<(ostream &,matiere);
	void ecrire();
	bool rechercher();
	void affiche();
	void ajouter(matiere );
};
matiere.ccp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
#include "matiere.h"
istream &operator>>(istream &in,matiere &mat)
{
 
	in>>mat.m_nom;
    cout<<"\t";
	in>>mat.m_cof;
	return in;
}
ostream &operator<<(ostream &out, matiere mat)
{
	out<<endl;
	out<<mat.m_nom;
	out<<"\t";
	out<<mat.m_cof<<endl;
	return out;
}
 
void matiere::ecrire()
{
	ofstream fichier("../matier.txt",ios::app|fstream::out);
		fichier<<*this;
	fichier.close();
}
 
void matiere::affiche()
{
	matiere mat;
	ifstream fichier("../matier.txt",fstream::in);
	string ligne ="";
	while(!fichier.eof())
	{ 
		getline(fichier,ligne);
		cout<<ligne;
		cout<<endl;
	}
	fichier.close();
}
bool matiere::rechercher()
{
	matiere mats;
	bool existe = false;
	ifstream fichier("../matier.txt",ios::app);
	string ligne;
	while(getline( fichier, ligne))
	{
		fichier>>mats;
		if(m_nom==mats.m_nom)
			existe=true;
	}
	return existe;
}
 
void ajouter(matiere mat)
{
	if(!mat.rechercher())
	{
		ofstream fichier("../matier.txt",ios_base::app|ios::out);
	    fichier<<mat;
	    fichier.close();
	}
	else
		cout<<"existe";
}
Je suis maintenant bloquer sur une fonction qui me permet de changer le contenu d'une ligne du fichier matiere.txt
exemple
La 2eme ligne contient
fr 12
je veux le changer en
ang 15
Une autre fonction que je veux la réaliser. Une fonction qui rempli un objet matiere à partir du fichier matiere.txt. On prend un exemple:
j'ai un objet mats
je veux le remplir par la 4eme ligne du fichier matiere qui contient ces informations
math 30


S.V.P aider moi pour réaliser cette fonction
merci.