Click here to Skip to main content
Licence CPOL
First Posted 13 Aug 2009
Views 10,659
Downloads 63
Bookmarked 13 times

FileDiff2 Optimized

By | 13 Aug 2009 | Article
A file diff utility.

Introduction

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;

do
{
    // 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
        //
        do
        {
            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);

streamA.Close();
streamB.Close();

History

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

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
GeneralWorks, but that's it... PinmemberRasqual Twilight0:58 22 Aug '09  
GeneralMy solution Pinmemberpsouza4micronet6:31 14 Aug '09  
GeneralRe: My solution PinmemberMatthew Hazlett8:51 14 Aug '09  
GeneralRe: My solution Pinmemberpsouza4micronet10:33 20 Aug '09  
GeneralRe: My solution PinmemberMatthew Hazlett20:07 21 Aug '09  
GeneralRe: My solution PinmemberPeter Souza6:56 25 Aug '09  
GeneralRe: My solution PinmemberMatthew Hazlett8:09 25 Aug '09  
GeneralRe: My solution PinmemberPeter Souza8:15 25 Aug '09  
GeneralRe: My solution PinmemberMatthew Hazlett19:38 25 Aug '09  
GeneralSome thoughts... Pinmemberpsouza4micronet13:50 13 Aug '09  
GeneralRe: Some thoughts... PinmemberMatthew Hazlett14:52 13 Aug '09  
GeneralRe: Some thoughts... Pinmemberpsouza4micronet16:07 13 Aug '09  
GeneralRe: Some thoughts... PinmemberMatthew Hazlett16:41 13 Aug '09  
GeneralRe: Some thoughts... Pinmemberpsouza4micronet1:59 14 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
Web02 | 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