如何在C ++中搜索文本文件中的单词? [英] How Do I Search For A Word In A Text File In C++?

查看:72
本文介绍了如何在C ++中搜索文本文件中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一条包含我要找的单词的行。我已经能够在我的文本文件中搜索该单词,但我的目标是,如果在文本文件中找到该单词,那么它应该显示包含该单词的整行。我真的很困惑如何做到这一点?请有人建议我一些想法。







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.



           #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");
}

推荐答案

这个怎么样?

How about this?
cout << line;



或者我错过了什么?

BTW:你还没有定义 as string 变量。

Andi


Or am I missing something?
BTW: You have not defined the line as string variable.
Andi


这篇关于如何在C ++中搜索文本文件中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆