Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-04-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Compare lines of 2 txt files in C#

I would like to compare lines 2 txt files. I think to do loops, but how? Here is my code to read in 2 files:

Code:
StreamReader li = new StreamReader("list.txt" );
StreamReader li2= new StreamReader ("list2.txt" );
string li = li.ReadLine();
string li2 = li2.ReadLine();
while (li != null && li2 != null)
{
Console.Write(li);
Console.Write(li2);
}
Reply With Quote
  #2  
Old 11-04-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: Compare lines of 2 txt files in C#

You want to compare it to say? Display only those that are similar? You are aware that there are tools (and libs) made for that?
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 11-04-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Compare lines of 2 txt files in C#

I think you need to modify your code with something like this:

Code:
using (StreamReader li = new StreamReader("list.txt" ))
using (StreamReader li2 = new StreamReader("list2.txt" ))
{
    while (true)
    {
        if (li.EndOfStream || li2.EndOfStream)
            break;
        string liTxt = li.ReadLine();
        string li2Txt = li2.ReadLine();
        if (!liTxt.Equals(li2Txt))
            Console.WriteLine("Lines different!!!" );
    }
}
Reply With Quote
  #4  
Old 11-04-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Compare lines of 2 txt files in C#

Thanks for your code Ashok but I am sorry it fails at while loop. Can you enlighten me where is the problem? I am unable to rectify it.
Reply With Quote
  #5  
Old 11-04-2009
Member
 
Join Date: Feb 2008
Posts: 194
Re: Compare lines of 2 txt files in C#

That may be the error somewhere else in your program, just recheck your code. One more thing, you can also use do while loop which is much handy sometimes.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Compare lines of 2 txt files in C#"
Thread Thread Starter Forum Replies Last Post
Small lines appears on my PDF files! Dragonball Z Windows Software 7 06-01-2012 12:54 PM
C program to Compare two files Ram Bharose Software Development 6 29-12-2010 06:32 AM
Compare HD+ vs FHD Bon-hwa Hardware Peripherals 4 12-09-2010 07:12 AM
How to compare 2 XML files using Java Aanand Software Development 9 21-05-2010 11:04 AM
Compare two Excel files Bull50 Software Development 4 06-04-2009 07:14 PM


All times are GMT +5.5. The time now is 08:32 AM.