Results 1 to 5 of 5

Thread: Compare lines of 2 txt files in C#

  1. #1
    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);
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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?

  3. #3
    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!!!" );
        }
    }

  4. #4
    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.

  5. #5
    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.

Similar Threads

  1. Small lines appears on my PDF files!
    By Dragonball Z in forum Windows Software
    Replies: 7
    Last Post: 06-01-2012, 12:54 PM
  2. C program to Compare two files
    By Ram Bharose in forum Software Development
    Replies: 6
    Last Post: 29-12-2010, 06:32 AM
  3. Compare HD+ vs FHD
    By Bon-hwa in forum Hardware Peripherals
    Replies: 4
    Last Post: 12-09-2010, 07:12 AM
  4. How to compare 2 XML files using Java
    By Aanand in forum Software Development
    Replies: 9
    Last Post: 21-05-2010, 11:04 AM
  5. Compare two Excel files
    By Bull50 in forum Software Development
    Replies: 4
    Last Post: 06-04-2009, 07:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,275,863.94720 seconds with 17 queries