Click here to Skip to main content
11,921,218 members (62,240 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked

FileDiff2 Optimized

, 13 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A file diff utility.


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

Using the code

ASCIIEncoding Encode = new ASCIIEncoding();

// Open the files
FileStream streamA = File.OpenRead(args[0]);
FileStream streamB = File.OpenRead(args[1]);

// Get the stream length
// (so we don't have to caluculate this a million times)
long lenA = streamA.Length - 1;
long lenB = streamB.Length - 1;

// Read the bytes
int byteA;
int byteB;

    // Read the streams
    byteA = streamA.ReadByte();
    byteB = streamB.ReadByte();

    // Are they the same
    if (byteA != byteB)
        // Remember where we parked the car
        long startPos = streamB.Position;

        // Read streamB until we = StreamA
            byteB = streamB.ReadByte();
        while (byteA != byteB && streamB.Position <= lenB);

        // How long is the difference?
        long length = streamB.Position - startPos;

        // Read the bytes
        byte[] theseBytes = new byte[length];
        streamB.Seek(length * -1, SeekOrigin.Current);|
        streamB.Read(theseBytes, 0, (int)length);

        Console.WriteLine("Pos:{0}, Len:{1}, Str:{2}", startPos, 
                          length, Encode.GetString(theseBytes));
while (streamA.Position <= lenA && streamB.Position <= lenB);



  • Aug 12, 2009: Written.
  • Aug 13, 2009: Rewritten to be more explicit, and I modified the file seeking and some variables to bring this down form 57ms run time to 27ms run time.


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
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.

You may also be interested in...

Comments and Discussions

GeneralWorks, but that's it... Pin
Rasqual Twilight22-Aug-09 1:58
memberRasqual Twilight22-Aug-09 1:58 
GeneralMy solution Pin
psouza4micronet14-Aug-09 7:31
memberpsouza4micronet14-Aug-09 7:31 
GeneralRe: My solution Pin
Matthew Hazlett14-Aug-09 9:51
memberMatthew Hazlett14-Aug-09 9:51 
GeneralRe: My solution Pin
psouza4micronet20-Aug-09 11:33
memberpsouza4micronet20-Aug-09 11:33 
GeneralRe: My solution Pin
Matthew Hazlett21-Aug-09 21:07
memberMatthew Hazlett21-Aug-09 21:07 
GeneralRe: My solution Pin
Peter Souza25-Aug-09 7:56
memberPeter Souza25-Aug-09 7:56 
GeneralRe: My solution Pin
Matthew Hazlett25-Aug-09 9:09
memberMatthew Hazlett25-Aug-09 9:09 
GeneralRe: My solution Pin
Peter Souza25-Aug-09 9:15
memberPeter Souza25-Aug-09 9:15 
GeneralRe: My solution Pin
Matthew Hazlett25-Aug-09 20:38
memberMatthew Hazlett25-Aug-09 20:38 
GeneralSome thoughts... Pin
psouza4micronet13-Aug-09 14:50
memberpsouza4micronet13-Aug-09 14:50 
GeneralRe: Some thoughts... Pin
Matthew Hazlett13-Aug-09 15:52
memberMatthew Hazlett13-Aug-09 15:52 
GeneralRe: Some thoughts... Pin
psouza4micronet13-Aug-09 17:07
memberpsouza4micronet13-Aug-09 17:07 
GeneralRe: Some thoughts... Pin
Matthew Hazlett13-Aug-09 17:41
memberMatthew Hazlett13-Aug-09 17:41 
GeneralRe: Some thoughts... Pin
psouza4micronet14-Aug-09 2:59
memberpsouza4micronet14-Aug-09 2:59 

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.

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