Read line by line from data2.txt, compare with data1.txt lines, if match , remove , else append to a new file (result.txt) .
mathces are exact string matches, both are text files.
And i also need to make the directory of the file to be user input, so this program must be able to compare 2 files which user inputs and generate a report.
My code as follows.
-------------------
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs1("data1.txt");
ifstream ifs2("data2.txt");
ofstream ofs("result.txt");
string line1;
string line2;
while (!ifs2.eof())
{
getline(ifs2,line2);
while (!ifs1.eof()) {
getline(ifs1,line1);
if (line1!=line2) {
ofs << line1;
ofs << "\n";
}
else {
}
}
}
return 0;
}
-------------------------
data1.txt :
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
data2.txt
This is line 5
This is line 1
Result.txt should be:
---------------------
This is line 2
This is line 3
This is line 4