Users Online

· Guests Online: 152

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Compare Two Text Files

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.87 seconds
10,817,145 unique visits