力求最详细,最简单,最便捷。
代码执行前后效果说明:
内容对比:
执行前:
执行后:
代码:
#include<iostream>
using namespace boost::filesystem;
void main()
{
const char* path = "G:\\old.txt"; //要去重的文件path
ifstream infile; //读旧文件(old.txt)
infile.open(path); //调用open方法打开旧文件
std::string textline; //临时变量,获取每一行内容
std::set<std::string> ss; //使用set,因为该容器中的内容是不可重复的
while(std::getline(infile, textline))
{
ss.insert(textline);
}
ofstream outfile; //用于写new文件
outfile.open("G:\\new.text"); //打开新文件。注:没有new.txt时,会自动创建名为new.txt的文件
for(auto& s : ss)
{
outfile<<s<<std::endl; //输出重定向,输出ss中的每个元素
}
}