#include #include #include #include using namespace std; void main() { //Open "input1.txt" and "input2.txt" for reading. fstream fin1("input1.txt"); fstream fin2("input2.txt"); //Open "output1.txt" and "output2.txt" for writing - the ios::out parameter is required. This will overwrite if applicable. fstream fout1("output1.txt", ios::out); fstream fout2("output2.txt", ios::out); //Make sure both input and output files were opened properly. if (fin1 && fin2 && fout1 && fout2) { //Tell them we're comparing the lists. cout << "Comparing lists...\n\n"; //These are the 4 lists that we will need for this program. list mainList, excludeList, outputList, duplicateList; //This will hold each line of characters that we read and write. string nextLine = ""; //This is an infinite loop - it will only break (exit) from the loop when we've reached the end of the file. while (true) { //Read the next line from the file. getline(fin1, nextLine); //Add the next line of data to the main list. mainList.push_back(nextLine); //Check to see if we've reached the end of the file. If so, break out of the reading/writing while loop. if (!fin1) { break; } } //Repeat the procedure above, except use fin2 and excludeList this time. while (true) { //Read the next line from the file. getline(fin2, nextLine); //Add the next line of data to the exclude list. excludeList.push_back(nextLine); //Check to see if we've reached the end of the file. If so, break out of the reading/writing while loop. if (!fin2) { break; } } //Used to keep track of duplicates. bool isDuplicate; //Iterate through each element in mainList. for (list::iterator i = mainList.begin(); i != mainList.end(); ++i) { //Assume that a duplicate was not found until proven otherwise. isDuplicate = false; //Iterate through each element in excludeList. for (list::iterator j = excludeList.begin(); j != excludeList.end(); ++j) { //See if the mainList element and the excludeList element are duplicates. if ((string)*i == (string)*j) { //If so, set the boolean to true so that the mainList value isn't added to the outputList later. isDuplicate = true; //Instead, add the duplicate value to the duplicateList. duplicateList.push_back((string)*i); //No need to make any more comparisons for this particular mainList element. break; } } //Check to see if this mainList element was not a duplicate of any of the excludeList elements. if (!isDuplicate) { //Place this element in the outputList. outputList.push_back((string)*i); } } for (list::iterator i = outputList.begin(); i != outputList.end(); ++i) { fout1 << (string)*i << "\n"; } for (list::iterator i = duplicateList.begin(); i != duplicateList.end(); ++i) { fout2 << (string)*i << "\n"; } //Assume success since there were no file opening errors. cout << "Comparing lists successful! Check:\n\n\"output1.txt\" (for a filtered list) and\n\"output2.txt\" (for a list of duplicates found)."; } else { //The files were not opened properly - let the user know that it was unsuccessful. cout << "Error either opening \"input1.txt\" or \"input2.txt\" for reading or opening \"output1.txt\" or \"output2.txt\" for writing."; } //Close the files. fout2.close(); fout1.close(); fin2.close(); fin1.close(); //Output some new line characters before the program asks for a key press. cout << "\n\n"; system("pause"); }