Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display a line containing the word that i am looking for. I have been able to search for the word in my text file but my goal is that if the word is found in the text file then it should display the entire line containing the word. I am really confused about how to do that? Please can anyone suggest me some ideas.



C++
           #include <iostream>
           #include <string>
           #include <fstream>
           using namespace std;
int main()
{
               
             ifstream input;
		size_t pos;
              string line;

		input.open("t.txt");
		if(input.is_open())
		{
			while(getline(input,line))
			{
			 pos = line.find("hey");
			  if(pos!=string::npos) // string::npos is returned if string is not found
        {
            cout <<"Found!";
            break;
        }
			}
		}

system("pause");
}
Posted
Updated 28-Oct-19 11:24am
v3
Comments
PIEBALDconsult 19-Nov-14 16:00pm    
I'd simply use FIND at the command line.
Surajit Das 19-Nov-14 16:10pm    
@PIBELDconsult:- Thanks! I shall try that.

1 solution

How about this?
C++
cout << line;

Or am I missing something?
BTW: You have not defined the line as string variable.
Andi
 
Share this answer
 
v5
Comments
Surajit Das 19-Nov-14 16:09pm    
@Andi:- Thank you Andi. I really appreciate your help.
Sergey Alexandrovich Kryukov 19-Nov-14 16:39pm    
Ha-ha, 5ed.
—SA
Andreas Gieriet 19-Nov-14 17:00pm    
Thanks for your 5!
Hm, I seem to have spoiled the homework assignment... ;-)
Andi
Hack Baba 2-Dec-18 8:04am    
How to Do The Same Thing In Turbo C++ and search the word entered in multiple files. Please help. Need this asap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900