DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   search word in file (http://www.daniweb.com/forums/thread99975.html)

Tell Me Dec 6th, 2007 1:32 pm
search word in file
 
Hi,

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

thank you

vmanes Dec 6th, 2007 1:38 pm
Re: search word in file
 
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

Narue Dec 6th, 2007 1:38 pm
Re: search word in file
 
>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.

Tell Me Dec 7th, 2007 10:32 am
Re: search word in file
 
thank you


All times are GMT -4. The time now is 11:12 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC