Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#

File Compare Utility for Lean and Mean Contest

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
27 Aug 2009CPOL2 min read 27.2K   376   8   3
File Compare Utility for entry in the Lean and Mean competition

Introduction 

This is a candidate entry for The Code Project's Lean and Mean competition to write an efficient text diffing tool.

The problem can be described like this: Create an application that will calculate and display the changes between two text files as fast as possible using the least amount of memory possible. Provide timing data and maximum memory use data to prove you're the leanest and meanest..

My solution is developed in C#, that compares two files. The resulting program is compact and very fast, and scales perfectly with any file size. 

Screen Shots

This is the screen when we execute this application: 

This is the page when we compare two different files:

Analysis 

For making this application fast and simple, we first retrieve the location of two files. If the location and name of both these files are the same, then we assume that the content of these files is same.

In the second step, if the names of these two files are different we first retrieve the length of content of these two files. If there is a difference in their lengths, then the function returns false and we calculate time and give the message that the files are not the same. If the lengths of both these files are the same, then we start reading both files, and comparing the lines consecutively, as long as the lines in both files are identical, we continue without any problem. However as soon as there is a mismatch, we have to decide whether the current line in file1 has disappeared, or the current line in file2 has been inserted, or the one line has been edited to become the other. We show the result in a message box.

Using the Code

I basically have created a function FileCompare:

C#
private bool FileCompare(string file1, string file2)
        {
            int file1byte;
            int file2byte;
            FileStream fs1;
            FileStream fs2;

            if (file1 == file2)
            {
                return true;
            }

            fs1 = new FileStream(file1, FileMode.Open);
            fs2 = new FileStream(file2, FileMode.Open);

            if (fs1.Length != fs2.Length)
            {
                fs1.Close();
                fs2.Close();

                return false;
            }

            do
            {
                file1byte = fs1.ReadByte();
                file2byte = fs2.ReadByte();
            }
            while ((file1byte == file2byte) && (file1byte != -1));

            fs1.Close();
            fs2.Close();

            return ((file1byte - file2byte) == 0);
        }

This function takes as an input two files which have to be compared further and gives us the output in the form of a boolean value that is true or false.

The sample code that is described in this article performs a byte-by-byte comparison until it finds a mismatch or it reaches the end of the file.

The code also performs two simple checks to increase the efficiency of the comparison:

  • If both file references point to the same file, the two files must be equal.
  • If the size of the two files is not the same, the two files are not the same.

Points of Interest

This functionality is similar to the MS-DOS-based Fc.exe utility that is included with various versions of Microsoft Windows and Microsoft MS-DOS, and with some development tools.

This application takes less than 10 milliseconds to compare two files and it takes 40k memory for the processing of the full application.

History

  • 28th August, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Samsung India Electronics Pvt. Ltd.
India India
Gaurav Dudeja has done B-Tech from ABES Engg College, Ghaziabad, Uttar Pradesh, India . He is an interested, committed, creative Software professional having more than 3 years of solid experience in web-based and windows based solutions in Microsoft Technologies using .NET 2.0, .NET 3.0 , .NET 3.5, ASP.NET 2.0, ASP.NET 3.5 C# 2.0, AJAX, Web Services, MS SQL Server 2005, WSS (Windows Sharepoint Server 3.0 ). He is also an MCP (Microsoft Certified Professional) and MCTS (Microsoft Certified Technology Specialist) on Web Development (.NET 2.0 ). He has good knowledge of Object Oriented Programming, 3-Tier Architecture and Design Patterns as well as good command over IIS (IIS 5.1,IIS 6.0, IIS 7.0) and deployment of Application on Live Production Environment.

Comments and Discussions

 
AnswerSorry.. but i guess that it doesn't cover some basic stuffs Pin
Zamaro31-Aug-09 16:15
Zamaro31-Aug-09 16:15 
GeneralRe: Sorry.. but i guess that it doesn't cover some basic stuffs Pin
Gaurav Dudeja India31-Aug-09 17:56
Gaurav Dudeja India31-Aug-09 17:56 
GeneralNice but.... Pin
Vivek_Minhas30-Aug-09 20:52
Vivek_Minhas30-Aug-09 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.