码迷,mamicode.com
首页 > 其他好文 > 详细

剔除在另一个文件中出现的行

时间:2014-07-16 16:48:18      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   文件   os   数据   

有两个文件A,B。我想要一个文件C,C中含有的是B文件中剔除与A相同行的数据。

用C++编写,用到fstream,ifstream,set以及vector。

 

#include <fstream>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct strless : public std::binary_function<const char*, const char*, bool>
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) < 0;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{

    ifstream comparefile;
    comparefile.open("E:\\a\\tasks.txt",ios::in);
    set<char*,strless> sline;
    char (*lines)[100] = new char[200][100];
    int count = 0;
    while(!comparefile.eof())
    {
        
        comparefile.getline(lines[count],100);
        sline.insert(lines[count]);
        ++count;
    }
    comparefile.close();
    
    ifstream sourcefile;
    sourcefile.open("E:\\a\\ttt.txt",ios::in);
    char (*lines1)[100] = new char[1000][100];
    count = 0;
    vector<char*> vline;
    while(!sourcefile.eof())
    {
        sourcefile.getline(lines1[count],100);
        if(!sline.count(lines1[count]))
            vline.push_back(lines1[count]);
        ++count;
    }
    sourcefile.close();

    fstream targetfile;
    targetfile.open("E:\\a\\list.txt",ios::app);

    vector<char*>::iterator vitr = vline.begin();
    while(vitr!= vline.end())
    {
        targetfile<<*vitr<<endl;
        ++vitr;
    }
    targetfile.close();
    
    return 0;
}

剔除在另一个文件中出现的行,布布扣,bubuko.com

剔除在另一个文件中出现的行

标签:style   blog   color   文件   os   数据   

原文地址:http://www.cnblogs.com/dy-techblog/p/3848539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!