Good evening everyone,
Firstly, I just want to say this is not homework related. I graduated 15 years ago, but I dabble with C++ purely for a hobby these days but must be getting rusty in some areas.
I have a question with regard to string comparisons.
What I have are two lengthy text files each containing lines of pertinent data.
One file is an update of the other.
What I want to do is utilize a piece of code that reads in the updated file and the original file (fstream), run an outer loop that reads the first line of code in the updated file, run an inner loop that goes through each line of the original file to check for matches. If there is no match then the line from the updated file is part of the update, and this can be written to a holding file. If there is a match, then of course its a duplicate and no action need be taken, just move on to the next line of code in the outer loop.
Here is the code that I currently have. Its syntactically correct, but there is a logic flaw somewhere as I am still getting part of the original file written to the holding file which obviously I don't want to do.
For your information, in the program, RS.txt is the updated file and YULIA.txt is the original file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
fstream write;
int a = 0;
write.open ("RSO.txt", ios :: out);
string line1;
string line2;
ifstream myfile1 ("RS.txt");
ifstream myfile2 ("YULIA.txt");
if (myfile1.is_open())
{
while (! myfile1.eof())
{
getline (myfile1,line1);
while (! myfile2.eof())
{
getline (myfile2,line2);
if (line1 == line2)
{
a = 1;
}
}
if (a == 0)
{
write << line1 << endl;
}
a = 0;
myfile2.clear();
}
}
}
If anyone can shed some light onto what I must obviously be doing wrong, I would appreciate it.
Thank you and kind regards
Clive Sims