65,211
社区成员
发帖
与我相关
我的任务
分享ifstream in;
string filename, line;
for (int i = 0; i < (int)m_FileList.size(); i++)
{
in.open(m_FileList[i].c_str(), fstream::in);
if(!in)
{
return;
}
while(getline(in, line))
{
//m_BaseList.m_strKeyWord.c_str()为要查找的关键字
//现在只能搜索一个关键字,比如"你好"
//如何才实现能够搜索多个关键字,比如"你好 我好 他好",就是类似于页面搜索的方式?????
if(strstr(line.c_str(), m_BaseList.m_strKeyWord.c_str()))
{
m_NeedFileList.push_back(m_FileList[i]);
break;
}
}
in.clear();
in.close();
}string str = "你好 我好 他好";
//如何将这个字符串的三个单独字符串依次放到KeyWordList中?
vector<string> KeyWordList;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
using namespace std;
int main()
{
string m_FileList[] = {"in.txt","out.txt"};
vector<string> m_NeedFileList;
string key = "你好 我好 他好";
vector<string> keywords;
char* cp = strtok(const_cast<char*>(key.c_str()), " ");;
while(cp)
{
keywords.push_back(cp);//获得单个关键字
cp = strtok(NULL, " ");
}
vector<string>::iterator key_it;
ifstream in;
string filename, line;
bool flag;//是否搜索到
for (int i = 0; i < 2; i++)
{
in.open(m_FileList[i].c_str(), fstream::in);
if(!in)
{
cerr<<"error"<<endl;
exit(1);
}
flag = false;
while(getline(in, line))
{
key_it = keywords.begin();
while(key_it!=keywords.end())//循环查找每个关键字
{
if(strstr(line.c_str(), (*key_it).c_str()))
{
m_NeedFileList.push_back(m_FileList[i]);
flag = true;
break;
}
++key_it;
}
if(flag)
break;
}
in.clear();
in.close();
}
vector<string>::iterator it = m_NeedFileList.begin();
while(it!=m_NeedFileList.end()) //打印结果
{
cout<<*it<<endl;
++it;
}
return 0;
}