C# Program to Compare Two Text Files
Posted by Superadmin on August 13 2022 08:15:24

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.

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.

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