0

I am new here. Anyway. I am trying to search a word in a text file(which has 10 sentences) from a text file that consists of few keywords. Basically i am trying to find if one of the keywords in file2 consist in file1. I have tried but it seems like it compares by line instead of word. if anyone could help me with this? thanks.

int main()
{
  bool Found;
  int i = 0;
  int j = 0;

  string str;
  ifstream file;
  file.open("words.txt", ios::in | ios::binary);

  string str2;
  ifstream file2;
  file2.open("M4.txt", ios::in | ios::binary);

  while (!file.eof() && !Found) {
    getline(file, str, ' ');
  }

  while (!file2.eof() && !Found) {
    getline(file2, str2, ' ');
  }

  //  if (str == str2)
  if (file.get() == file2.get()) {
    cout << "This order is valid. M3" << endl;

  } else {
    cout << "This order is invalid. M3" << endl;
  }

  system("pause");
  return 0;
}

I hope anyone can help with this. Have stuck at here for weeks :(

  • `Found` is never initialized, so you already have undefined behavior going into your first loop. Even if you did initialize it to `false`, the loop would never end. `!file.eof()` [don't do this](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – user657267 May 21 '14 at 03:36
  • I think you are trying to find whether two lines contains common word or not. You can split your line and store words in a set, then do intersection to check the result set length. If length == 0, then "no", else "yes" – Robin May 21 '14 at 03:40
  • robin - what do you mean by If length == 0, then "no", else "yes" ? – user3659038 May 21 '14 at 11:28

1 Answers1

1

There are two problems here: how to split a file into tokens, and how to search for an element of a set, the standard library provides solutions for both.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

int main()
{
  using It = std::istream_iterator<std::string>;

  std::ifstream text_file("words.txt");
  std::ifstream words_file("M4.txt");

  std::vector<std::string> text{It(text_file), It()};
  std::vector<std::string> words{It(words_file), It()};

  bool result = std::find_first_of(
    text.begin(), text.end(), 
    words.begin(), words.end()
  ) != text.end();

  std::cout << result << '\n';
}

If you need to know which words match, you can either use std::sets, or sort the vectors, and then use std::set_intersection to create a new range

std::vector<std::string> result;

std::sort(text.begin(), text.end());
std::sort(words.begin(), words.end());

std::set_intersection(
  text.begin(), text.end(),
  words.begin(), words.end(),
  std::back_inserter(result)
);

for (auto& s : result)
  std::cout << s << '\n';
user657267
  • 19,343
  • 5
  • 50
  • 73
  • first of all, thank you for the reply! :) when i was trying to run you code, it seems like having some errors. [Error] #include nested too deeply, stream was not declared. and i cant even compile my previous code. why is that happening? – user3659038 May 21 '14 at 05:53
  • @user3659038 I may have missed an `#include`, Visual Studio seems to complain without `string`, if you were using VS try the code above again, if not, remember to pass the `std=c++11` flag when compiling. – user657267 May 21 '14 at 07:14
  • okay I have been studying about the std::sort and also std::set_interactions. But i dont quite understand how to use it. Sorry Im a bit slow. But can you please lead me on how to use it? – user3659038 May 21 '14 at 09:43
  • std::sort (text); std::sort (words); it=std::set_intersection(text, words_file, It.begin()); It.resize(it-It.begin()); std::cout << "The intersection has " << (It.size()) << " elements:\n"; for (it=It.begin(); it!=It.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; – user3659038 May 21 '14 at 10:28
  • @user3659038 I've edited my answer, you probably need to read a good introductory C++ book as from your comment it looks like you do not know how `sort` works. – user657267 May 21 '14 at 11:53
  • omg thank you very much! now im editing on how i want it to be. and yes i need to brush up my c++ :/ – user3659038 May 23 '14 at 16:47