I have two text files that I am trying to compare (test.txt and removewords.txt. If a word from removetext.txt is found in test.txt it should be excluded from the output; otherwise it should be displayed. Here is my code:

while ( inClientFile >> word ) 
 {
   while ( inIgnoreFile >> ignoreWord )
   {  
      int result = word.compare( ignoreWord );
      if (result == 0) 
      {            
         //erase word from output  
         //i have tried to use word.erase to no avail as well finding 
         //the length of the string "word" and deleting it that way.
      } 
      else
          cout << word << endl;
   }
 }

Is there a better way to go about this than what I am trying here?

I don't think this code works because once the cursor in inIgnoreFile goes to EOF, you aren't resetting it to the beginning of the file for the next loop. Try using the seekg() function(Google for info).

To get the proper text in the output, you'll have to make some changes. The cout statement needs to outside the inner while loop.

To actually remove the word from the file, you would always make a temporary file, then rename it at the end. Or you can save all the text to memory, then rewerite the file.

Is there a better way to go about this than what I am trying here?

Yes. Read in all the words to ignore into an array.
Then read in a word at a time from text.txt and see if the word is in the ignore list.
If the word is not in the list, output it. If it is, don't.

Yes. Read in all the words to ignore into an array.
Then read in a word at a time from text.txt and see if the word is in the ignore list.
If the word is not in the list, output it. If it is, don't.

Ah yes, a string array would make things faster.

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.