0

Hi,

I want to ask if there function in library of c++ to search word in file ??

thank you

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Tell Me
0

Not that I know of. You have to read some (all) of the file, then search using any of several possible methods.

If you expand on your problem definition, and show the work you've done to solve it, you will get better help here.

Please read the forum rules.

Val

0

>I want to ask if there function in library of c++ to search word in file ??
No. You need to break it down into smaller operations like reading a file and searching a string for a word. Then you can do this:

std::ifstream in ( "myfile" );

if ( in ) {
  bool found = false;
  std::string search_for = "teapot";
  std::string word;

  while ( in>> word ) {
    if ( word == search_word ) {
      found = true;
      break;
    }
  }

  //...
}

Or this:

std::ifstream in ( "myfile" );

if ( in ) {
  bool found = false;
  std::string search_for = "teapot";
  std::string line;

  while ( std::getline ( in, line ) ) {
    if ( word.find ( search_word ) != std::string::npos ) {
      found = true;
      break;
    }
  }

  //...
}

For example.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.