上代码,读取了快递.txt、招聘.txt、电商.txt,目前输出结果是:
www.sohu.com 快递
www.baidu.com 快递
www.yijia.com 快递
www.goole.com 招聘
www.reset.com 招聘
www.baidu.com 招聘
www.baidu.com 电商
www.easy.com 电商
www.suhu.com 电商
还未实现的功能是把上面的输出去重存储到文件,像下面所示:
www.sohu.com 快递
www.baidu.com 快递 招聘 电商
.
.
不知道该怎么去重并输出,求解惑,人比较笨,最好详细点
#include <atlstr.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <io.h>
#include <set>
using namespace std;
void GetLineAndPrint(string in_name)
{
//string tname;
ifstream fin(in_name);
int l = in_name.size();
string tname(in_name.substr(0,l-4));
int pos1 = tname.find_last_of('\\');
string name(tname.substr(pos1+1));
//ofstream fout("1.txt",ios::app);
typedef set<string> set_t;
//set_t s;
if(!fin)
{
cerr<<"open file error"<<endl;
exit(-1);
}
string str;
string s;
while(getline(fin,str))
{
s.assign(str);
set_t str;
str.insert(name);
set_t st;
st.insert(s);
for (set_t::const_iterator p = str.cbegin(); p != str.cend(); ++p)
cout <<s <<" "<< *p <<endl;
}
}
int main()
{
struct _finddata_t fileinfo;
string in_path;
string in_name;
cin>>in_path;
string curr = in_path+"\\*.txt";
long handle;
if((handle=_findfirst(curr.c_str(),&fileinfo))==-1L)
{
cout<<"没有找到匹配文件!"<<endl;
system("pause");
return 0;
}
else
{
in_name = in_path + "\\" + fileinfo.name ;
GetLineAndPrint(in_name);
while(!(_findnext(handle,&fileinfo)))
{
in_name = in_path + "\\" +fileinfo.name;
GetLineAndPrint(in_name);
}
_findclose(handle);
}
system("pause");
return 0;
}