Made a small project to help compare two files that I am testing...but for some reason the line I highlighted isn't working as its supposed to. I originally had
c1 != c2
and then changed it to
!strcmp(c1, c2)
Maybe its a dumb logic bug, but would appreciate some help :)
// Compare two files to see if the contents exactly match
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
int main(int argc, char *argv[])
{
std::string error1 = "Not enough parameters passed.";
std::string error2 = "Invalid file name passed.";
std::string error3 = "File sizes do not match.";
std::string error4 = "Files have different contents.";
std::cout << "Running comparison program." << std::endl;
try
{
if (argc != 3)
throw error1;
std::cout << "Filename 1: " << argv[1] << std::endl;
std::cout << "Filename 2: " << argv[2] << std::endl;
std::ifstream inFile1(argv[1], std::ios::in);
std::ifstream inFile2(argv[2], std::ios::in);
if (!inFile1.is_open())
throw error2;
if (!inFile2.is_open())
throw error2;
inFile1.seekg(0, std::ios::end);
std::streamsize len1 = inFile1.tellg();
inFile1.seekg(0, std::ios::beg);
inFile2.seekg(0, std::ios::end);
std::streamsize len2 = inFile2.tellg();
inFile2.seekg(0, std::ios::beg);
if (len1 != len2)
throw error3;
char *c1 = new char;
char *c2 = new char;
char *cArray1 = new char[20];
char *cArray2 = new char[20];
inFile1.read(c1, 1);
inFile2.read(c2, 1);
/***** ERROR HERE *****/
if ( !strcmp(c1, c2) )
{
std::cout << "c1: " << c1 << " c2: " << c2 << std::endl;
throw error4;
}
for (std::streamsize i = 1; i < len1; i++)
{
delete c1;
delete c2;
c1 = new char;
c2 = new char;
inFile1.read(c1, 1);
inFile2.read(c2, 1);
if (c1 != c2)
{
i -= 20;
inFile1.read(cArray1, 20);
inFile2.read(cArray2, 20);
for (int j = 0; j < 20; j++)
std::cout << cArray1[j];
std::cout << std::endl;
for (int j = 0; j < 20; j++)
std::cout << cArray2[j];
std::cout << std::endl;
throw error4;
}
}
delete c1;
delete c2;
inFile1.close();
inFile2.close();
std::cout << "The two files match exactly." << std::endl;
return 0;
}
catch (std::string e)
{
std::cout << "Exception: " << e << std::endl;
}
}