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

Dani AI

Generated

asked for user-selectable files and exact string comparison; provided a working vector-based approach. A simpler, faster, and more robust pattern is to load the lines to remove into an unordered_set (fast membership tests) and then stream through the main file, writing only lines that are not in the set. This preserves the original order of the main file, avoids nested reads or rewinding, and keeps complexity near O(n) on average instead of O(n*m).

Common pitfalls observed in earlier replies: using while(!ifs.eof()) is unreliable; erasing from a vector while iterating can skip elements or be inefficient; failing to check that file streams opened successfully; and not handling trailing \r from Windows files (which makes lines not match exactly). If exact one-to-one removal of duplicated lines is required, use a multiset or track counts instead of a plain unordered_set.

Example implementation (C++11+) that accepts file paths from the user, strips trailing CR, and writes non-matching lines to an output file:

#include <iostream>
#include <fstream>
#include <string>
#include <unordered_set>

static void chomp(std::string &s) {
    if (!s.empty() && s.back() == '\r') s.pop_back();
}

int main() {
    std::string keepPath, removePath, outPath;
    std::cout << "Path of file to filter: ";
    std::getline(std::cin, keepPath);
    std::cout << "Path of file with lines to remove: ";
    std::getline(std::cin, removePath);
    std::cout << "Output path: ";
    std::getline(std::cin, outPath);

    std::ifstream remF(removePath);
    if (!remF) { std::cerr << "Cannot open remove file\n"; return 1; }
    std::unordered_set<std::string> toRemove;
    std::string line;
    while (std::getline(remF, line)) { chomp(line); toRemove.insert(line); }

    std::ifstream inF(keepPath);
    std::ofstream outF(outPath);
    if (!inF || !outF) { std::cerr << "Cannot open input/output\n"; return 1; }
    while (std::getline(inF, line)) {
        chomp(line);
        if (toRemove.find(line) == toRemove.end()) outF << line << '\n';
    }
}

Notes: use a case-fold or trimmed version of the strings for case-insensitive or whitespace-agnostic matching; switch to a count map if removals should be limited to the same number of occurrences; consider std::filesystem::path (C++17) for richer path handling.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	ifstream ifs1("data1.txt");
	vector<string> data1;
	if (ifs1.is_open()){
		string line;
		while(getline(ifs1, line)){
			data1.push_back(line);
		}
	}
	else{
		cout << "Unable to open file data1.txt";
		return 1;
	}
	ifstream ifs2("data2.txt");
	if (ifs2.is_open()){
		string line;
		while(getline(ifs2, line)){
			for(size_t i = 0; i < data1.size(); ++i){
				if (line == data1[i])
					data1.erase(data1.begin()+i);
			}
		}
	}
	else{
		cout << "Unable to open file data2.txt";
		return 1;
	}
	ofstream ofs("result.txt");
	if (ofs.is_open()){
		for(size_t i = 0; i < data1.size(); ++i){
			ofs << data1[i] << endl;
		}
	}
	else{
		cout << "Unable to open file result.txt";
		return 1;
	}
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.