[2013.8.29]马甲去重复 c++源码

#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
#include <vector>
using namespace std;

int main()
{
  try
  {
    string ifile;
    cout << "请输入要去重复的文件" << endl;
    cin >> ifile;
    cin.sync();

    string ofile;
    cout << "请输入要保存的文件"<<endl;
    cin >> ofile;
    cin.sync();

    fstream ifs(ifile, ios::in);
    if(!ifs) throw exception("源文件打开失败!");
    fstream ofs(ofile, ios::out | ios::append);
    if(!ofs)
    {
      ifs.close();
      throw exception("目标文件打开失败");
    }

    vector removed;
    while(!ifs.eof())
    {
      string tmp;
      ifs >> tmp;
      bool exist = false;
      for(int i = 0; i < removed.size(); i++)
      {
        if(removed[i] == tmp)
        {exist = ture; break;}
      }

      if(!exist)
      {
        removed.push_back(tmp);
        ofs << tmp << endl;
      }
    }

    ifs.close();
    ofs.close();
  }
  catch(exception &ex)
  {cout <<  ex.what() << endl;}

  system("pause");
  return 0;
}
posted @ 2014-05-16 18:36  绝不原创的飞龙  阅读(10)  评论(0编辑  收藏  举报  来源