473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
4 2799
Robert W. <Ro*****@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
I know. Was just checking!
--
Robert W.
Vancouver, BC
www.mwtech.com

"Jon Skeet [C# MVP]" wrote:
Robert W. <Ro*****@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
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.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #4
Robert W. <Ro*****@discussions.microsoft.com> wrote:
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++;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
4
by: bob lambert | last post by:
Help I am trying to deploy to another pc a vb.net std 2002 windows form application. I am confused. I created a project - windows form I built form, compiled and debugged. I created a...
27
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
0
by: Andy Dingley | last post by:
I have a problem involving lots of simple text files (Java properties files), for which I'm building Python tools to manage their contents. I'm also writing lots of Python modules and using...
3
by: bg_ie | last post by:
Hi, I currently have a class called DefinedTest which relates to a set of tests I perform. But there are two Test states, those that are defined and those that are not. I'd therefore like to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.