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

That while loop is incorrect -- you don't need to use eof()

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 ( getline (myfile1,line1) )
    {
                myfile2.seekp(0, ios::beg); // back to beginning of file
                myfile2.clear();
                while (getline (myfile2,line2) )
                {
                      if (line1 == line2)
                      {
                          write << line1 << endl;
                          break; // stop looking
                       } 
                 }
              }
          }
}

AD's solution is a good one.

I'd just like to explain what I see going wrong with the original code.
First off, eof( ) should never be used until something's been attempted to read - it's meaningless on first testing, and will sometimes cause last item read to be processed twice.

The inner loop seems to just spin around, reading all the way through the file with no action. Then you compare to the first file.

The use of myfile2.clear() is clearing the eof flag of that stream, so you'll keep attempting to read that one, getting an empty string as the result, and keep comparing the empty string to the first file's content.

An extremely ineffective algorithm. It has O(M*N) complexity and can't detect line swapping and line deleting and inserting...
Think about better algorithm now (for example, load a file into map<string,vector<int>> or what else...

This article has been dead for over six months. Start a new discussion instead.