File Comparator Fails when I enter Files
Usually when I'm programming I'll get some compiler errors. Maybe even a logic error or two, but this is the first time this happened.
Approximately 9:00am EST I ran my program, happily delighted that I got all of the compile time errors out of it and then this happens.
As soon as I enter the file names the whole thing blows up in my face!
I checked the code over again and again and I can't seem to find the problem. It runs fine until I enter the file names. Then the whole thing shuts down. Can anyone help me out! D;
Here I'll give you the Complete source. It is well annotated. I was trying to make a File Comparator, but as soon as you enter the files the program crashes, some comparator right o_O? Thanks in advanced.
Code:
/* File Comparator
Created by: Shaun Carter (why06)
Date: 10/03/2009*/
#include <iostream>
#include <fstream>
using namespace std;
int CompareFiles(ifstream &file1, ifstream &file2);
int main(int argc, char argv[])
{
char* file[2];
char* files;
bool filesCompared = false;
while(!filesCompared)
{ // Starting message
cout << "Enter: File1,File2\n";
cin >> files;
// Will try to parse the text into different files
try{
int p = 0;
for(int i = 0; files; i++)
{
if(files[i]==',')
{
p++; continue;
}
else file[p]+=files[i];
if(p>1)throw(1);
}
// If more then two files are given this will cathc it.
}catch(int){cout << "Only compare two files" << "This program will ONLY compare the firt two you entered\n" ; return 1;}
//Declarinng my fstream variables that I will use for the binary comparison//
ifstream f1(file[0], ios::in | ios::binary); //
if(!f1){ //
cout << "Cannot open first file.\n"; //
return 1; //
} //
ifstream f2(file[1], ios::in | ios::binary); //
if(!f2){ //
cout << "Cannot open second file.\n"; //
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 returs result of Comparison.
if(result)
{
cout << "Files are not the same." << "\n";
if(result == 1)cout << file[1] <<" is larger then " << file[0] << ".\n";
else cout << file[0] <<" is larger then " << file[1];
}
else cout << "These files are the same.";
filesCompared = true;
}
system("pause");
return 0;
}
int CompareFiles(ifstream &file1, ifstream &file2)
{
char buf1[50];
char buf2[50];
file1.read( buf1, sizeof buf1);
file2.read( buf2, sizeof buf2);
if(file1.gcount() != file2.gcount())
{
cout << "Differing sizes...\n";
if(file1.gcount() > file2.gcount()) return 1;
else return 2;
}
else
{
cout << "Same size. Running binary comparison...";
// Left off here
return 0;
}
}