采用Boost::filesystem操作文件

// BoostTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "boost/filesystem.hpp"   // includes all needed Boost.Filesystem declarations
#include <iostream>               // for std::cout
using namespace boost::filesystem; 
using namespace std;


#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#include <iostream>

namespace fs = boost::filesystem;

void getAllFileOfPath(std::string strPath,std::vector<std::string>& dllNames)
{


	fs::path fullPath(fs::initial_path());        // 初始化为本项目路径
	fullPath = fs::system_complete(fs::path(strPath,fs::native));    //将相对路径转换为绝对路径

	// 注:fullPath 使用相对路径、绝对路径都可行

	if (!fs::exists(fullPath))            //路径是否存在
	{
		std::cout << "找不到指定目录,请检查该目录是否存在:";
		std::cout << fullPath.native_file_string() << std::endl;        // 输出路径名
		return;
	}
	if (!fs::is_directory(fullPath))        // 是否是目录
	{
		std::cout << "指定路径不是目录";
		std::cout << fullPath.native_file_string() << std::endl;
		return;
	}

	fs::directory_iterator end_iter;
	for (fs::directory_iterator file_itr(fullPath); file_itr != end_iter; ++file_itr)
	{
		if (!fs::is_directory(*file_itr) && (fs::extension(*file_itr)!=".exe"))        // 文件后缀
		{
			dllNames.push_back(file_itr->leaf());    //获取文件名
		}
	}


}


int main() 
{
	try
	{
		string p("C:/TDDOWNLOAD/说岳全传/说岳全传/fdsaf");
		std::vector<std::string> dllNames;
		getAllFileOfPath(p,dllNames);

		std::vector<std::string>::iterator itr = dllNames.begin();
		for (;itr != dllNames.end(); ++itr)
		{
			std::cout<<*itr<<std::endl;
			remove(p+"/"+*itr);
		}
	}
	catch (exception &e)
	{
		cout<<e.what()<<endl;
	}
}

原文地址:https://www.cnblogs.com/lilun/p/1813023.html