C# Program to Compare Two Text Files

This is a C# Program to perform file comparison.

Problem Description

This C# Program Performs File Comparison.

Problem Solution

Here the files are compared and based on the equality the results are displayed.

Program/Source Code

Here is source code of the C# Program to Perform File Comparison. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform File Comparison
 */
using System;
using System.Threading;
using System.IO;
 
class Reader
{
    string fileName;
    public string data;
 
    public Reader(string fn) { fileName = fn; }
 
    public void Read()
    {
        FileStream s = new FileStream(fileName, FileMode.Open);
        StreamReader r = new StreamReader(s);
        data = r.ReadToEnd();
        r.Close();
        s.Close();
 
    }
}
class ThreadSample
{
    static void Main(string[] arg)
    {
        if (arg.Length == 2)
        {
            Reader a = new Reader(arg[0]);
            Reader b = new Reader(arg[1]);
            Thread ta = new Thread(new ThreadStart(a.Read));
            Thread tb = new Thread(new ThreadStart(b.Read));
            ta.Start();
            tb.Start();
            ta.Join();
            tb.Join();
 
            if (a.data.Length == b.data.Length)
            {
                int i = 0;
                while (i < a.data.Length && a.data[i] == b.data[i]) i++;
                if (i == a.data.Length)
                Console.WriteLine("Files {0} and {1} are equal", arg[0], arg[1]);
                else
                Console.WriteLine("Files {0} and {1} are not equal", arg[0], arg[1]);
            }
            else
            {
                Console.WriteLine("Files {0} and {1} are not equal", arg[0], arg[1]);
            }
        }
        else
        {
            Console.WriteLine("-- enter two file names");
        }
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to perform file comparison. Using if condition statements check the length of the string is equal to 2. If the condition is true then we are executing the statement. Create two thread classes using ‘ta’ and ‘tb’ variable.

advertisement
advertisement

The start() method is used to start the thread and the join() method is used to block the calling thread until a thread terminates while continuing to perform standard COM and SendMessage pumping. If else condition statement is used to check that the value of ‘a’ variable length is equal to the value of ‘b’ variable length.

If the condition is true then execute the statement. While loop is used to check that the value of ‘a’ variable length is equal to the value of ‘b’ variable length is less than the value of ‘i’ variable using logical AND operator. If the condition is true then execute the iteration of the loop.

If else condition statement is used to check the value of ‘a’ variable length is equal to the value of ‘i’ variable. If the condition is true then execute if condition statement. Print the statement as both the files are equal. Otherwise, if the condition is false then execute the statement and print the statement as both the files are not equal.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
 
D:\Desktop\c#\program codes>pgno382.exe d:\\sri\\File1.txt d:\\sri\\File1.txt
Files d:\\sri\\File1.txt and d:\\sri\\File1.txt 
are equal

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.