Search in a file

Task is to search for a certain texts in a file in c++
I need to create a file and put some text inside it and then later on search for a particular word and then perform some operations and then start search for different word from where I left.

what logic should i apply I am not getting.
Can anyone help
TIA
Hi there,

First you will need to create an object of ifstream to read the file:

1
2
3
#include <fstream>
#include <string>
ifstream ifs;


Then you open the file:

ifs.open("filename");

Then you read every word into a string and check against the search-word:

1
2
3
4
5
6
7
8
std::string tmp_word;

while (ifs.good())
{
    ifs >> tmp_word;
    if (tmp_word.compare(search_word) == 0)
    { /* ... */ }
}


Hopefully that will get you on your way.

All the best,
NwN
Thanks a lot mate......
I have got it.
Topic archived. No new replies allowed.