Hi,
I am trying to write a C++ code to write a file with the similar data between two input files.Input files will be command line arguments.Following is the code ,I am able to open & create files but getting errors during comparison & there is no explicit error message I am getting please let me know where I am going wrong.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class diff {
private:
ifstream infile1,infile2;
fstream samedata;
public:
diff (char *,char *,char *);
void write_file ();
};
diff::diff (char * file1, char * file2, char *samefile) {
infile1.open(file1,ios::binary);
infile2.open(file2,ios::binary);
samedata.open(samefile,ios::in | ios::out | ios::trunc);
/*if (infile1.is_open() || infile2.is_open() || samedata.is_open())
{
cout << "File Opened Succesfully !!!" <<endl;
}*/
/*infile1.close();
infile2.close();
samedata.close();*/
}
void diff::write_file () {
string line1,line2;
if (infile1.is_open() && infile2.is_open())
{
while (!infile1.eof())
{
getline (infile1,line1);
if (line1.empty())
break;
while (!infile2.eof())
{
getline (infile2,line2);
if (line2.empty())
break;
if (line1.compare(line2) == 0)
{
samedata << line1 << "\n";
}
}
infile2.clear();
infile2.seekg(0,ios::beg);
}
}
}
int main (int argc,char *argv[])
{
char *samedatafile="samedata.txt";
string line1;
diff diffobj(argv[0],argv[1],samedatafile);
diffobj.write_file();
return 0;
}