#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; }