matching text in a file

My question is how do you get a program to go through a text file line by line until it find a match and then displays a message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#inclue <fstream>

string user_input;

using namespace std;

int main()
{
while ( user_input != '#')
{
getline(cin, user_input)
	fstream inout;
	inout.open("Example",ios::out|ios::app);
	inout<< user_input <<endl;
	inout.close();

	string Example;
	fstream inout;
	inout.open("Example.txt", ios::in);
	while (!inout.eof())
	{
	inout>>Example;
	if( user_input == Example)
	{
		cout<<"try again!!!"<<endl;
	}
	inout.close();
	}
	return 0;
Last edited on
string::find() http://www.cplusplus.com/reference/string/string/find/?kw=string%3A%3Afind

The most basic form probably is :

1
2
3
4
5
6
ifstream fin( "text.txt" );
string line;
while( getline( fin, line ) ) {
    if( line.find( "blah" ) != string::npos ) // if found in line then
        cout << "Found \"blah\"" << endl; // do something
}
Topic archived. No new replies allowed.