qq_36777482
qq_36777482
采纳率0%
2017-03-03 08:57 阅读 2.2k

c++ 里面关于读取多个文件内容并去重

50

上代码,读取了快递.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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • yangbo50304 yangbo50304 2017-03-03 08:59

    一个问题咋还问多次呢?

     #include <atlstr.h>
    #include <winbase.h>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <list> 
    #include <fstream>  
    #include <vector>
    #include <cstring>
    #include <io.h> 
    using namespace std;
    
    
    
    typedef struct tagMember
    {
        char* name;
        char* group;
    }Member;
    // tagMember url[500];
    //struct tagMember url[9];
    
    Member url[] =
    {
        { "搜索","www.baidu.com" },
        { "搜索","www.goole.com" },
        { "购物","www.baidu.com" },
        { "购物","www.kuaidi.com" },
        { "赌博","www.baidu.com" },
    };
    
    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 pos = in_name.find('.');
        //if(pos>0)
        //   tname = in_name.left(pos);
        int pos1 = tname.find_last_of('\\');
        string name(tname.substr(pos1 + 1));
        ofstream fout("1.txt", ios::app);
        if (!fin)
        {
            cerr << "open file error" << endl;
            exit(-1);
        }
        string str;
        int i = 0;
        while (getline(fin, str))
        {
            fout << name << ' ' << str << endl;
            url[i].name = new char[str.length()]; //str.data();
            url[i].group = new char[str.length()];
            memset(url[i].name, 0, str.length());
            memset(url[i].group, 0, str.length());
            int spaceindex = str.find(' ');
            if (spaceindex != str.npos)
            {
                memcpy_s(url[i].name, spaceindex + 1, str.c_str(), spaceindex + 1);
                memcpy_s(url[i].group, str.length() - spaceindex - 1, str.substr(spaceindex + 1).c_str(), str.length() - spaceindex - 1);
            }
            i++;
        }
        fin.close();
        fout.close();
    }
    
    
    
    
    typedef list<char*> ListType;
    typedef struct ListNode
    {
        char* groupName;
        ListType *next;
    }NodeType;
    
    typedef list<NodeType*> myList;
    typedef myList::iterator myIter;
    
    
    myIter myFind(myList& mylist, char* dest)
    {
        myIter it;
        for (it = mylist.begin(); it != mylist.end(); it++)
        {
            //if ((*it)->groupName == dest)
            if (strcmp((*it)->groupName, dest) == 0)
            {
                return it;
            }
        }
        return it;
    }
    
    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;
            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);
        }
    
    
    
        myList classInfo;
    
        NodeType* pNode;
        //插入节点
        for (size_t i = 0; i < 5; i++)
        {
            //创建一个索引节点
            myIter it = myFind(classInfo, url[i].group);
            if (it != classInfo.end())    //已存在
            {
                //存储name
                (*it)->next->push_back(url[i].name);
            }
            else    //创建新的NodeType节点
            {
                pNode = new NodeType;
                pNode->groupName = url[i].group;
                pNode->next = new ListType;    //创建list
    
                                               //存储当前的name
                pNode->next->push_back(url[i].name);
                classInfo.push_back(pNode);
            }
        }
    
        //输出:
        for (myIter it = classInfo.begin(); it != classInfo.end(); it++)
        {
            cout << (*it)->groupName << ": ";
            for (ListType::iterator bg = (*it)->next->begin();
                bg != (*it)->next->end(); bg++)
            {
                cout << (*bg) << "\t";
            }
            cout << endl;
        }
    
        system("pause");
        return 0;
    }
    
    点赞 评论 复制链接分享
  • shen_wei shen_wei 2017-03-03 09:20

    那现在的问题是啥。。。应该详细说明白。。。。

    点赞 评论 复制链接分享
  • shen_wei shen_wei 2017-03-03 09:42
  • GKatHere GKatHere 2017-03-05 02:56

    使用

     std::map<std::string, std::vector<std::string>> pack;
     输出前面的名称,再遍历vector输出即可
    
    点赞 评论 复制链接分享

相关推荐