Is there a simple way to test if 2 text files are identical?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've scanned through the various methods of the 'File' library but can't see
any method that would be equivalent to the DOS "COMP" command. Is there, in
fact, a simple method to do this or do I have to write my own?
 
Robert W. said:
I've scanned through the various methods of the 'File' library but can't see
any method that would be equivalent to the DOS "COMP" command. Is there, in
fact, a simple method to do this or do I have to write my own?

Yes, you have to write your own code to do this. It's not terribly hard
though.
 
Jon,

I followed your advice and wrote my own file comparison checker. I'll show
the code below as it may help out someone else. One concern I had was
reading byte by byte but the speed seems to be very fast. Here's the code:

/// <summary>
/// Compares two files to see if they're identical.
/// </summary>
/// <param name="file1"></param>
/// <param name="file2"></param>
/// <returns>true - files are identical; false - they're not
identical</returns>
public static bool CompareFiles(string file1, string file2)
{
bool noMatch = false;

// Check filesizes first
FileInfo fileInfo1 = new FileInfo(file1);
FileInfo fileInfo2 = new FileInfo(file2);

if (fileInfo1.Length == fileInfo2.Length)
{
// Check actual file contents, character by character.
FileStream fileStream1 = fileInfo1.OpenRead();
FileStream fileStream2 = fileInfo2.OpenRead();
bool eofReached = false;

do
{
int val1 = fileStream1.ReadByte();
int val2 = fileStream2.ReadByte();

if (val1 == -1 || val2 == -1)
eofReached = true;
else if (val1 != val2)
noMatch = true;

} while (!noMatch && !eofReached);

fileStream1.Close();
fileStream2.Close();
}
else
return false;

if (noMatch)
return false;

return true;
}
 
Robert W. said:
I followed your advice and wrote my own file comparison checker. I'll show
the code below as it may help out someone else. One concern I had was
reading byte by byte but the speed seems to be very fast. Here's the code:

A few things:

1) I'd use a using statement to close the streams even if an exception
occurs
2) It would definitely be faster to use chunked reading - but if it's
fast enough, keeping with simple code is definitely a good thing :)
3) You're actually comparing the files byte by byte, not character by
character. That's probably okay for you, but it's worth being aware
of.

I wrote the following code a while ago, but it may be helpful to you:

const int BufferLength = 32768;
static bool CompareStreams (Stream s1, Stream s2)
{
if (s1==null || s2==null)
{
throw new ArgumentNullException
("Streams to compare must both be non-null");
}

// A buffer for each stream
byte[] buffer1 = new byte[BufferLength];
byte[] buffer2 = new byte[BufferLength];

// Number of bytes valid within each buffer
int buffer1Valid=0;
int buffer2Valid=0;

// Index within the buffer for each stream
int buffer1Index=0;
int buffer2Index=0;

while (true)
{
// Read any more data if we need to
if (buffer1Index==buffer1Valid)
{
buffer1Valid = s1.Read(buffer1, 0, BufferLength);
buffer1Index=0;
}

if (buffer2Index==buffer2Valid)
{
buffer2Valid = s2.Read(buffer2, 0, BufferLength);
buffer2Index=0;
}

// We've read to the end of both streams simultaneously
if (buffer1Valid==0 && buffer2Valid==0)
{
return true;
}

// We've read to the end of one stream but not the other
if (buffer1Valid==0 || buffer2Valid==0)
{
return false;
}

if (buffer1[buffer1Index] != buffer2[buffer2Index])
{
return false;
}

buffer1Index++;
buffer2Index++;
}
}
 
Back
Top