何处流连 2017-03-03 08:57 采纳率: 0%
浏览 2324
已结题

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

上代码,读取了快递.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 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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制