File Comparator

Posts 1–7 of 7 · Page 1 of 1
File Comparator
Yeh well this is my sucky file comparator. I've been working on. It should have taken me all of 10 minutes to make, but instead it took me all of ten days o_O. I got a little confused between C++ char* and strings in Java, but now I understand it thanks to BA and the program seems to work okay. As far as I am aware it can only compare non-executable files. I might work on it again someday, but I have spent all the time on it I think I should have. Since the project is rather pointless and I have already gained the concept I'm going to move on. Here is the source:

[php]
/* File Comparator
Created by: Shaun Carter (why06)
Date: 10/03/2009*/

#include <iostream>
#include <fstream>
using namespace std;

int CompareFiles(fstream &file1, fstream &file2);

int main()
{

char file1[50];
char file2[50];

bool filesCompared = false;

while(!filesCompared)
{
// Starting message
cout << "Enter: File1\n";
cin >> file1;
cout << "Enter: File2\n";
cin >> file2;


//Declarinng fstream variables that I will use for the binary comparison//
cout << "Opening: " << file1 <<endl;
fstream f1((const char*)file1, ios::binary | ios::in | ios:ut);
if(!f1){
cout << "Cannot open " <<file1 << endl;
system("pause");
return 1;
}
cout << "Opening: " << file2 << endl;
fstream f2((const char*)file2, ios::binary | ios::in | ios:ut);
if(!f2){
cout << "Cannot open " << file2 << endl;
system("pause");
return 1;
}
/////////////////////////////////////////////////////////////////////////////

//Execution passes to CompareFiles. Will return 0 (are equal), 1 (file1 is larger), 2 (file2 is larger)
cout << "Comparing...\n";
int result = CompareFiles(f1, f2);

// Finally returns result of Comparison.

if(result)
{

cout << "Files are not the same." << "\n";
if(result == 1)cout << file1 <<" is larger then " << file2 << ".\n";
else if( result == 3) cout << "binary conctructions are not the same.\n";
else cout << file2 <<" is larger then " << file1 << endl;
}
else cout << "These files are the same.\n";
filesCompared = true;
}
system("pause");
return 0;
}

int CompareFiles(fstream &file1, fstream &file2)
{
char buf1[50];
char buf2[50];

file1.read( (char*) buf1, sizeof buf1);
file2.read( (char*) buf2, sizeof buf2);

if(file1.gcount() != file2.gcount())
{
cout << "Differing sizes...\n";
if(file1.gcount() > file2.gcount()){ file1.close(); file2.close(); return 1;}
else return 2;
}
else
{
cout << "Same size. \nRunning binary comparison...\n";
// Binary comparison
char f1,f2;
while(file1.get(f1))
{
file2.get(f2);
if(f1 != f2 ) {cout << "These files are not the same.\n"; return 3;}
}
cout << "Finished Binary Comparison.\n";
file1.close();
file2.close();
return 0;
}
}

[/php]
I took zeco's idea and used the php tags to make it colored.
File Comparator.rarattachment deleted · 1 downloads before removal
Great that where getting some more people interested in coding.

Gj dude

idk wtf all this shit is, but gj
Nice to see other people posting their source code.

You don't have to worry about it right now, but when you start working with large projects you should get into the habit of writing a header for each source file. I.e

/**
* Written by so and so
* Copyright information
* Short description of file
*/

* edit *
Mind you, you did. My bad.
Quote Originally Posted by radnomguywfq3 View Post
Mind you, you did. My bad.
Hehe xD.

I'm trying to get a good grasp of some of the basic I/O stuff before I move on. I went over that chapter pretty quick, but I had a bad feeling that it would turn out to be really important farther down the road....

Oh, and thanks legend
nice job

Code:
fstream f1((const char*)file1, ios::binary | ios::in | ios::out);
is this for opening the files?

if you got the result you can also make a switch because you got more than 2 options. it is less confusing for reading the code
Quote Originally Posted by lalakijilp View Post
nice job

Code:
fstream f1((const char*)file1, ios::binary | ios::in | ios::out);
is this for opening the files?

if you got the result you can also make a switch because you got more than 2 options. it is less confusing for reading the code
Yeh that part is for opening file1. The ios::binary, ios::in, and ios::out are all flags I am setting to open the file in a certain configuration. It specifies that I want to open it in binary mode and that I want to be able to read from the file (ios::in) and write to the file (ios::out).

I could have used a switch, yes. I rushed that part and it looks bad. :P
lol jetamay

good job why
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us