C++对文件内容去重(最详细,最简单)

力求最详细,最简单,最便捷。
代码执行前后效果说明:

内容对比:在这里插入图片描述

执行前:
去重之前的文件

执行后:
去重之后生成的图片

代码:

#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中的每个元素
	}
}


发布了21 篇原创文章 · 获赞 3 · 访问量 609

猜你喜欢

转载自blog.csdn.net/W96866/article/details/105655737