[C#] Comparer les lignes de 2 fichiers txt

Comparer les lignes de 2 fichiers txt [C#] - C#/.NET managed - Programmation

Marsh Posté le 07-03-2009 à 02:51:08    

Bonjour,
 
je voudrais comparer les lignes de 2 fichiers txt.
Je pense qu'il faut faire des boucles mais comment??  :??:  
 
Voici mon code pour lire dans les 2 fichiers :
 

Code :
  1. StreamReader li = new StreamReader("liste.txt" );
  2. StreamReader li2= new StreamReader ("liste2.txt" );
  3. string li = li.ReadLine();
  4. string li2 = li2.ReadLine();
  5. while (li != null && li2 != null)
  6. {
  7. Console.Write(li);
  8. Console.Write(li2);
  9. }

Reply

Marsh Posté le 07-03-2009 à 02:51:08   

Reply

Marsh Posté le 07-03-2009 à 09:16:29    

tu veux les comparer, c'est à dire ? n'afficher que celles qui sont semblables ?


---------------
J'ai un string dans l'array (Paris Hilton)
Reply

Marsh Posté le 07-03-2009 à 13:47:42    

Tu es au courant qu'il y a des outils (et libs) faits pour ça?


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 15-03-2009 à 16:47:35    

Code :
  1. using (StreamReader li = new StreamReader("liste.txt" ))
  2. using (StreamReader li2 = new StreamReader("liste2.txt" ))
  3. {
  4.     while (true)
  5.     {
  6.         if (li.EndOfStream || li2.EndOfStream)
  7.             break;
  8.         string liTxt = li.ReadLine();
  9.         string li2Txt = li2.ReadLine();
  10.         if (!liTxt.Equals(li2Txt))
  11.             Console.WriteLine("Lignes différentes !!!" );
  12.     }
  13. }


---------------
Another .Net Blog
Reply

Marsh Posté le 10-04-2009 à 14:59:29    

haazheel a écrit :

Code :
  1. ...
  2.     while (true)
  3.     {
  4.        ...


 

FAIL.

Message cité 1 fois
Message édité par tomsoft le 10-04-2009 à 14:59:46
Reply

Marsh Posté le 10-04-2009 à 15:04:07    


 :non:  

Code :
  1. while (true)
  2. {
  3.     if (li.EndOfStream || li2.EndOfStream)
  4.          break;


[:fail]

Spoiler :

en fait non, c'est le bien connu while-if-break pattern, spécialiste des pyramides depuis 1972 [:jar jar]


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 10-04-2009 à 15:12:30    

a partir du moment ou je vois un while true,  
 
c'est FAIL.
 
pour debugguer a la limite, mais pas plus :o

Reply

Marsh Posté le 10-04-2009 à 16:17:01    

tomsoft a écrit :

a partir du moment ou je vois un while true,  
 
c'est FAIL.
 
pour debugguer a la limite, mais pas plus :o


Bah non, il y a des use cases valides: de la gestion/consommation d'évènements par exemple, ou quand ton break se retrouve au milieu de deux bouts de code au sein du while (donc qu'à chaque tour tu dois executer un peu avant et un peu après).
 
C'est également pratique quand t'as un while mais pas de do{}while, mais c'est pas le cas ici :o


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 11-04-2009 à 10:16:57    

ok :jap:

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed