Click here to Skip to main content
Licence CPOL
First Posted 12 Aug 2009
Views 7,203
Downloads 60
Bookmarked 9 times

FileDiff Contest Entry

By | 12 Aug 2009 | Article
Text Difference between two files

Introduction

This is a contest entry for file differences.

Using the Code

This application is pretty basic. It uses FileStream objects to perform its task.

ASCIIEncoding encode = new ASCIIEncoding();
FileStream fileA = File.OpenRead(args[0]); 
FileStream fileB = File.OpenRead(args[1]);

int b = 0;
int l = 0;

fileA.Position = 0;
fileB.Position = 0;

We start off by opening the files and setting the positions within the files to 0. The var b is the last byte read and the var l is the length of the changed bytes. 

while (fileA.Position <= (fileA.Length - 1))
{
    b = fileA.ReadByte();

    if (fileB.Position <= (fileB.Length - 1))
    {
        if (b != fileB.ReadByte())
        {
            l = 1;

            while (fileB.Position <= (fileB.Length - 1) && 
                fileB.ReadByte() != b)
            l += 1;

            byte[] s = new byte[l];
            fileB.Seek(fileB.Position - l, 0);
            fileB.Read(s, 0, l);
 
            Console.WriteLine("FileDiff Pos:{0}, Len{1}, Str:{2}",
                              fileA.Position, 
                              l, 
                              encode.GetString(s));
        }
    }
}

fileA.Close();
fileB.Close(); 

This is the main application loop. As you can see, it steps through the file byte by byte. When two bytes are different, it stops looking and scans stream B for the next byte that's equal to stream A.

Points of Interest

This is a contest entry, written in C# with .NET v2, is 73 lines including blank lines / comments and formatted code + the timers, etc. The number of lines that are not overhead, blank or comments are 28. It uses 4,384K Memory (Private Working Set) and the EXE is 5.5k.

Run-time is roughly 30 milliseconds, output is the position in the file, length of the Diff and the textual representation. 

License

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

About the Author

Matthew Hazlett

Web Developer

United States United States

Member

I started programming for fun when I was about 10 on an Franklin Ace 1000.
 
I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)
 
The way I learned was by example, now its time to give back to the next generation of coders.
 



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA file diff should be symmetric. PinmemberTRK37:50 18 Aug '09  
GeneralMy vote of 2 Pinmemberpocheptsov7:03 18 Aug '09  
GeneralMy vote of 1 Pinmembercrayzeecoder22:40 14 Aug '09  
QuestionBut what... is it good for? PinmemberPIEBALDconsult18:39 12 Aug '09  
AnswerRe: But what... is it good for? PinmemberMatthew Hazlett19:06 12 Aug '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Aug 2009
Article Copyright 2009 by Matthew Hazlett
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid