Microsoft KB Archive/816189

= How to create a FileCompare function by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816189

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-







For a Microsoft Visual C# .NET version of this article, see 320348.



For a Microsoft Visual Basic .NET version of this article, see 320346.



This article refers to the following Microsoft .NET Framework Class Library namespace:

System.IO

IN THIS TASK
 SUMMARY  Create the Sample Run the Sample

REFERENCES



SUMMARY
This step-by-step article describes how to compare two files to see if their contents are the same. This comparison looks at the contents of the two files, but does not at the file names, locations, dates, times, or other attributes.

This functionality is similar to the MS-DOS-based Fc.exe utility that is included with various versions of Microsoft Windows and Microsoft MS-DOS, and with some development tools.

The sample code that is described in this article performs a byte-by-byte comparison until a mismatch is found, or until the end of the file is reached. The code also performs the following two simple checks to increase the efficiency of the comparison:
 * If both file references point to the same file, the two files must be equal.
 * If the size of the two files different, the two files are different.

back to the top

Create the Sample
<ol> Start Visual Studio .NET (2003) or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> Click Visual C++ Projects under Project Types, and then click Empty Project (.NET) under Templates. Name the project FileCompare.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Empty Project under Templates.</li> In Solution Explorer, right-click FileCompare, point to Add, and then click Add New Item.</li> In the Add New Item dialog box, click Header File under Templates. In the Name text box, type Form1, and then click Open. The Form1.h file is displayed.</li>  Paste the following code in the Form1.h file:
 * 1) pragma once
 * 2) using <mscorlib.dll>
 * 3) using <system.windows.forms.dll>
 * 4) using <system.dll>
 * 5) using <system.drawing.dll>

namespace Compare {   using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Drawing; using namespace System::IO;

public __gc class Form1 : public System::Windows::Forms::Form {      public: Form1(void) {           InitializeComponent; }   protected: void Dispose(Boolean disposing) {           if (disposing && components) {               components->Dispose; }           __super::Dispose(disposing); }   private: System::Windows::Forms::TextBox *  textBox1; private: System::Windows::Forms::TextBox * textBox2; private: System::Windows::Forms::Label * label1; private: System::Windows::Forms::Label * label2; private: System::Windows::Forms::Button * Compare;

private:

System::ComponentModel::Container * components;

void InitializeComponent(void) {           this->textBox1 = new System::Windows::Forms::TextBox; this->textBox2 = new System::Windows::Forms::TextBox; this->label1 = new System::Windows::Forms::Label; this->label2 = new System::Windows::Forms::Label; this->Compare = new System::Windows::Forms::Button; this->SuspendLayout; //            // textBox1 //            this->textBox1->Location = System::Drawing::Point(32, 104); this->textBox1->Name = S&quot;textBox1&quot;; this->textBox1->TabIndex = 0; this->textBox1->Text = S&quot;&quot;; //            // textBox2 //            this->textBox2->Location = System::Drawing::Point(168, 104); this->textBox2->Name = S&quot;textBox2&quot;; this->textBox2->TabIndex = 1; this->textBox2->Text = S&quot;&quot;; //            // label1 //            this->label1->Location = System::Drawing::Point(32, 64); this->label1->Name = S&quot;label1&quot;; this->label1->TabIndex = 2; this->label1->Text = S&quot;File1&quot;; //            // label2 //            this->label2->Location = System::Drawing::Point(176, 64); this->label2->Name = S&quot;label2&quot;; this->label2->TabIndex = 3; this->label2->Text = S&quot;File2&quot;; //            // Compare //            this->Compare->Location = System::Drawing::Point(96, 168); this->Compare->Name = S&quot;Compare&quot;; this->Compare->TabIndex = 4; this->Compare->Text = S&quot;Compare&quot;; this->Compare->Click += new System::EventHandler(this, button1_Click); //            // Form1 //            this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(292, 273); this->Controls->Add(this->Compare); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->Name = S&quot;Form1&quot;; this->Text = S&quot;Form1&quot;; this->ResumeLayout(false);

}   private: System::Void button1_Click(System::Object * sender, System::EventArgs *  e)        { }

}; } This code creates a form that contains two text box controls and a command button.

Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li>  Add the following function to the Form1 class: // This method accepts two strings that represent two files to // compare. A return value of 0 indicates that the contents of the files // are the same. A return value of any other value indicates that the // files are different. private: bool FileCompare(String *file1, String *file2) {   int file1byte; int file2byte; FileStream *fs1; FileStream *fs2; // Determine if the same file was referenced two times. if (String::Equals(file1,file2)) {       // Return true to indicate that the files are the same. return true; }

// Open the two files. try {       fs1 = new FileStream(file1, FileMode::Open); fs2 = new FileStream(file2, FileMode::Open); // Check the file sizes. If the sizes are different, the files // are different. if (fs1->Length != fs2->Length) {           // Close the file fs1->Close; fs2->Close;

// Return false to indicate files are different return false; }

// Read and compare a byte from each file until either a       // non-matching set of bytes is found or until the end of        // file1 is reached. do        { // Read one byte from each file. file1byte = fs1->ReadByte; file2byte = fs2->ReadByte; }       while ((file1byte == file2byte) && (file1byte != -1));

// Close the files. fs1->Close; fs2->Close; }   catch(Exception *ex) {       if(fs1) {           fs1->Close; }       if(fs2) {           fs2->Close; }       throw ex; }

// Return the success of the comparison. &quot;file1byte&quot; is    // equal to &quot;file2byte&quot; at this point only if the files are // the same. return ((file1byte - file2byte) == 0); } </li>  Paste the following code in the Click event of the button1_Click command button in the Form1 class: try {   if (FileCompare(this->textBox1->Text, this->textBox2->Text)) {       MessageBox::Show(&quot;Files are equal.&quot;); }   else {       MessageBox::Show(&quot;Files are not equal.&quot;); } } catch(Exception *ex) {   MessageBox::Show(ex->Message); } </li> In Solution Explorer, right-click FileCompare, point to Add, and then click Add New Item.</li> In the Add New Item dialog box, click C++ File under Templates. In the Name text box, type File_Compare, and then click Open. The File_Compare.cpp file is displayed.</li>  Paste the following code in the File_Compare.cpp file:
 * 1) include &quot;form1.h&quot;


 * 1) include <windows.h>
 * 2) include <tchar.h>

using namespace Compare; </li>  Add the following WinMain function to the File_Compare.cpp file: int APIENTRY _tWinMain(HINSTANCE hInstance,                    HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow) { } </li>  Paste the following code in the WinMain function: System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; Application::Run(new Form1); return 0; </li> <li>Save the sample.</li></ol>

back to the top

Run the Sample

 * 1) Supply full paths to two files in the text box controls.
 * 2) Click Compare.

back to the top

<div class="references_section">