Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
To check for a word in a text file line by line and when found print the whole line which is containing the word .Pleas help as fast as possible.

What I have tried:

here is the code:-
Here cw is a pointer having the line stored in it,w is the word to be searched for, gw is also a pointer used to store the word obtained from each line.

while (!o.eof())
{
o.getline(cw,'\n');
for(i=0;cw[i]!='\0';i++)
{
gw[i]=cw[i];
}
// if(cw[i]==w[i])
if(gw==w)
cout<<cw;
if(o.eof())break;
}
o.close();
Posted
Updated 16-Jan-21 20:45pm
v2

C++
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream ifs("in.txt");

  string word = "foo";
  string line;

  while( getline(ifs, line ))
  {
    size_t pos = line.find(word);
    if ( pos != string::npos)
      cout << line << endl;
  }
}


[update]
If you don't want foofighters, that is if you need to search your word between word-boundaries (as hinted by yourself and KarstenK) then you might use a regular expression:
C++
#include <iostream>
#include <fstream>
#include <regex>
using namespace std;

int main ()
{
  ifstream ifs("in.txt");

  string word = "foo";

  std::regex e{"\\b" + word + "\\b"};

  string line;

  while( getline(ifs, line ))
  {
    if ( regex_search( line, e) )
      cout << line << endl;
  }
}

[/update]
 
Share this answer
 
v2
Comments
KarstenK 14-Nov-18 9:33am    
The code may have some flaw like, when searching for "foo" and matching the maysterious "foofigthers" ;-)
CPallini 14-Nov-18 12:21pm    
I am aware of it. As a matter of fact, it is by design, you know, it is a feature. :-D
Vishal Banerjee 21-Nov-18 4:50am    
Thank you very much for the code help and support but I want to mention here that I am unable to check this code as my compiler is not supporting this feature.
CPallini 21-Nov-18 13:34pm    
Apparently you need a more modern compiler.
Hi,

I wrote a little function in C++ to solve your problem. Please check it out and if it worked press that little green button to accept the solution.

C++
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::cout << "Write the path of the file\n";
	std::string path;
	std::cin >> path;

	std::ifstream file(path.c_str());

	if (file.is_open())
	{
		std::cout << "File '" << path << "' opened.\n";

		std::cout << "Write the word you're searching for\n";
		std::string word;
		std::cin >> word;
		std::string line;

		std::string candidate;

		while (!file.eof()) // for each candidate word read from the file 
		{
			std::getline(file, line);
			std::size_t found = line.find(word);
			if (found != std::string::npos)
				std::cout << "The word '" << word << "' has been found in line " << line;
		}
		file.close();
	}

	else
	{
		std::cerr << "Error! File not found!\n";
		return 1;
	}
}


If this did not solve your problem then please leave a comment and I will assist you by improving my solution until your problem gets solved.

Cheers,
AH
 
Share this answer
 
v3
Comments
Vishal Banerjee 15-Nov-18 3:11am    
Sorry Sir I am unable to understand the solution.
Vishal Banerjee 15-Nov-18 5:19am    
Sir the code provided by you is having error in its first line only the first header file is not supported by the compiler.
Aydin Homay 15-Nov-18 5:21am    
I wrote the code in Visual Studio so if you open it there it will work otherwise you need to replace that header. Could you please let me know which editor are you using?
Vishal Banerjee 19-Nov-18 5:12am    
Sir please reply.
Vishal Banerjee 16-Nov-18 5:09am    
Sir, I am use turbo c++, Dev c++ and code blocks 16.01 only pls try to make the code compatible accordingly.

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