C# Program to Perform File Comparison

«
»

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.

advertisement
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

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.

advertisement
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
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn