Ce forum est maintenant fermé, seule cette archive statique reste consultable.
  FORUM Rue-Montgallet.com
  Programmation
  C - C++

  fichier txt

Bas de page
Auteur Sujet :

fichier txt

n°19662
esprit2009
Profil : Jeune recrue
Posté le 07-08-2009 à 00:17:42  
 

bonjour,
j'ai réalisé au cours de mon mini projet cette classe  
le.h

Code :
  1. #include<iostream>
  2. #include<string>
  3. #include <fstream>
  4. using namespace std;
  5. class matiere
  6. {
  7. string m_nom;
  8. int m_cof;
  9. public:
  10. matiere():m_nom(" " ),m_cof(0){} ;
  11. matiere(string nom,int cof):m_nom(nom),m_cof(cof){};
  12. friend istream &operator>>(istream &,matiere & );
  13. friend ostream &operator<<(ostream &,matiere);
  14. void ecrire();
  15. bool rechercher();
  16. void affiche();
  17. void ajouter();
  18. void changer(matiere);
  19. int getmatiere();
  20. };


 
le .cpp

Code :
  1. #include "matiere.h"
  2. istream &operator>>(istream &in,matiere &mat)
  3. {
  4. in>>mat.m_nom;
  5.     cout<<"\t";
  6. in>>mat.m_cof;
  7. return in;
  8. }
  9. ostream &operator<<(ostream &out, matiere mat)
  10. {
  11. out<<endl;
  12. out<<mat.m_nom;
  13. out<<"\t";
  14. out<<mat.m_cof<<endl;
  15. return out;
  16. }
  17. void matiere::ecrire()
  18. {
  19. ofstream fichier("../matier.txt",ios::app|fstream::out);
  20.  fichier<<*this;
  21. fichier.close();
  22. }
  23. void matiere::affiche()
  24. {
  25. matiere mat;
  26. ifstream fichier("../matier.txt",fstream::in);
  27. string ligne ="";
  28. while(!fichier.eof())
  29. {
  30.  getline(fichier,ligne);
  31.  cout<<ligne;
  32.  cout<<endl;
  33. }
  34. fichier.close();
  35. }
  36. bool matiere::rechercher()
  37. {
  38. matiere mats;
  39. bool existe = false;
  40. ifstream fichier("../matier.txt",ios::app);
  41. string ligne;
  42. while(getline( fichier, ligne))
  43. {
  44.  fichier>>mats;
  45.  if(m_nom==mats.m_nom)
  46.   existe=true;
  47. }
  48. return existe;
  49. }
  50. void matiere::ajouter()
  51. {
  52. if(!(*this).rechercher())
  53. {
  54.  ofstream fichier("../matier.txt",ios_base::app|ios::out);
  55.     fichier<<*this;
  56.     fichier.close();
  57. }
  58. else
  59.  cout<<"existe";
  60. }
  61. void matiere::changer(matiere mte)
  62. {
  63. ifstream fichier("../matier.txt",fstream::in);
  64. ofstream fichier2("../matier_tmp.txt",fstream::out);
  65.     matiere m,ms,mss;
  66.     while(!fichier.eof())
  67. {
  68.   fichier>>m;
  69.   if(m.m_nom==(*this).m_nom)
  70.    fichier2<<mte;
  71.   else if(m.m_nom!=ms.m_nom)
  72.    fichier2<<m;
  73. }
  74.    fichier.close();
  75.    fichier2.close();
  76.    remove("../matier.txt" );
  77.    rename("../matier_tmp.txt","../matier.txt" );
  78. }
  79. int matiere::getmatiere()
  80. {
  81. return m_cof;
  82. }


 
une fonction main pour tester les différents fonctions de la classe matiere
 

Code :
  1. #include "matiere.h"
  2. void main()
  3. {
  4. matiere mat;
  5. for(int i=0;i<3;i++)
  6.   {
  7.              cout<<"donner le nom de matiere et son coefficiant"<<endl;
  8.          cin>>mat;
  9.       mat.ecrire();
  10.   }
  11.  mat.affiche();
  12.  matiere mats("C#",12);
  13.  matiere mate("fr",0);
  14.  if(mats.rechercher())
  15.   cout<<"matiere existe"<<endl;
  16.  else
  17.   cout<<"not exist"<<endl;
  18.  mate.changer(mats);
  19.  cout<<endl<<"----------------------------------------------------"<<endl;
  20.  mats.affiche();
  21. }


 
j'ai un problème  dans la fonction changer() qui change l'objet qui a appelé cette fonction par  l'objet passe en paramètre.  
le problème est que la dernière ligne est toujours écrit 2fois.
Merci d'avance pour votre aide.
 

mood
Pub
Posté le 07-08-2009 à 00:17:42  
 

n°19663
cmoila
Profil : Membre
Posté le 07-08-2009 à 12:01:42  
 

Code :
  1. void matiere::changer(matiere mte)
  2. {
  3.    ifstream fichier("../matier.txt",fstream::in);
  4.    ofstream fichier2("../matier_tmp.txt",fstream::out);
  5.    matiere m, ms, mss;
  6.    while ( !fichier.eof() ) {
  7.        fichier >> m ;
  8.        if ( m.m_nom == (*this).m_nom )  fichier2 << mte ;
  9.        else if ( m.m_nom != ms.m_nom )  fichier2 << m ;
  10.    }
  11.  
  12.    fichier.close();
  13.    fichier2.close();
  14.    remove("../matier.txt" );
  15.    rename("../matier_tmp.txt","../matier.txt" );
  16. }


 
Tiens du C++ ! Ça nous change.
 
1ere chose à faire rendre le bout de code lisible, afin de pouvoir le lire.
 
2eme, se demander où est initialisé l'objet "ms" utilisé ligne 10 ?
 
3eme, a quoi sert "mss" ?


Message édité par cmoila le 07-08-2009 à 12:05:14
n°19664
esprit2009
Profil : Jeune recrue
Posté le 08-08-2009 à 18:04:40  
 

je m'excuse pour le code illisible.
 

Code :
  1. void matiere::changer(matiere mte)
  2.     {
  3.         ifstream fichier("../matier.txt",fstream::in);
  4.         ofstream fichier2("../matier_tmp.txt",fstream::out);
  5.         matiere m;
  6.         while ( !fichier.eof() )
  7.           {
  8.              fichier >> m ;
  9.              if ( m.m_nom == (*this).m_nom ) 
  10.                   fichier2 << mte ;
  11.              else
  12.               fichier2 << m ;
  13.           }
  14.         fichier.close();
  15.         fichier2.close();
  16.         remove("../matier.txt" );
  17.         rename("../matier_tmp.txt","../matier.txt" );
  18.       }


 
j'espère que maintenant soit mieux.
Merci.


Message édité par esprit2009 le 08-08-2009 à 18:13:48
n°19665
cmoila
Profil : Membre
Posté le 08-08-2009 à 20:25:44  
 

Un fichier ne devient eof  (end of file)  qu'après une tentative de lecture qui a échouée.  
 
 

Code :
  1. void matiere::changer(matiere mte)
  2. {
  3.    ifstream fichier("../matier.txt",fstream::in);
  4.    ofstream fichier2("../matier_tmp.txt",fstream::out);
  5.    matiere m;
  6.        
  7.   while ( true )  {
  8.       fichier >> m ;
  9.       if ( !fichier.eof() ) {   
  10.          if ( m.m_nom == (*this).m_nom )  fichier2 << mte ;
  11.          else                             fichier2 << m ;
  12.       }
  13.       else  break;
  14.   }
  15.   fichier.close();
  16.   fichier2.close();
  17.   remove("../matier.txt" );
  18.   rename("../matier_tmp.txt","../matier.txt" );
  19. }


Message édité par cmoila le 08-08-2009 à 20:29:49
n°19666
esprit2009
Profil : Jeune recrue
Posté le 09-08-2009 à 23:34:04  
 

Merci cmoila
votre solution est impicable
1000 merci  

n°19667
cmoila
Profil : Membre
Posté le 10-08-2009 à 22:49:33  
 

La solution vraiment impeccable c'est celle là :

Code :
  1. void matiere::changer(matiere mte)
  2. {
  3.   ifstream fichier("../matier.txt",fstream::in);
  4.   ofstream fichier2("../matier_tmp.txt",fstream::out);
  5.   matiere m;
  6.  
  7.   while ( !( fichier >> m ).eof() )  {
  8.     if ( m.m_nom == (*this).m_nom )  fichier2 << mte ;
  9.     else                             fichier2 << m ;
  10.   }
  11.  
  12.   fichier.close();
  13.   fichier2.close();
  14.   remove("../matier.txt" );
  15.   rename("../matier_tmp.txt","../matier.txt" );
  16. }


Mais j'avais peur que se soit un peu abstrait.

  FORUM Rue-Montgallet.com
  Programmation
  C - C++

  fichier txt

© 2000-2024 Forum.rue-montgallet.com - Tous droits réservés