使用boost filesystem 库实现枚举指定目录下的所有jpeg图片;

#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

bool scanFilesUseRecursive(const string& rootPath,vector<string>& container=*(new vector<string>()))
{    

	namespace fs = boost::filesystem;    
	fs::path fullpath (rootPath, fs::native);   

	vector<string> &ret = container;   

	//判断路径是否存在
	if(!fs::exists(fullpath))
	{
		return false;
	}    
	
	fs::recursive_directory_iterator end_iter;   

	for(fs::recursive_directory_iterator iter(fullpath);iter!=end_iter;iter++)
	{    

		try{            
			//是文件名&&.jpg
			if (!fs::is_directory( *iter ) &&(fs::extension(*iter)==".jpg"||fs::extension(*iter)==".JPG"||fs::extension(*iter)==".jpeg"||fs::extension(*iter)==".JPEG") )
			{
				//
				std::string jpgstring=iter->path().string();
			    
				ret.push_back(jpgstring);                
				std::cout << *iter << " is a file" << std::endl;       
			}
			
		} catch ( const std::exception & ex )
		{            
			std::cerr << ex.what() << std::endl;           
			continue;        
		}    
	} 

	return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
	std::string path="D:\\TestTool\\";

	vector<string> FileName;
	
	scanFilesUseRecursive(path,FileName);

	return 0;
}


 

常用函数

 

函数名                                           作用

system_complete(path);           返回完整路径(相对路径+当前路径)
exists(path);                                目录是否存在
is_directory(path);
is_directory(file_status);            是否是路径
is_regular_file(path);
is_regular_file(file_status);             是否是普通文件
is_symlink(path);
is_symlink(file_status);                   是否是一个链接文件
file_status status(path);                  返回路径名对应的状态
initial_path();                                 得到程序运行时的系统当前路径
current_path();                              得到系统当前路径
current_path(const Path& p);         改变当前路径
space_info space(const Path& p);     得到指定路径下的空间信息,space_info capacity, free available三个成员变量,分别表示容量,剩余空间和可用空间。
last_write_time(const Path& p);    最后修改时间
last_write_time(const Path& p, const std::time_t new_time);  修改最后修改时间
bool create_directory(const Path& dp);      建立路径
create_hard_link(const Path1& to_p, const Path2& from_p);
error_code create_hard_link(const Path1& to_p, const Path2& from_p, error_code& ec); 建立硬链接
create_symlink(const Path1& to_p, const Path2& from_p);
create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec);  建立软链接
remove(const Path& p, system::error_code & ec = singular );      删除文件
remove_all(const Path&p);    递归删除p中所有内容,返回删除文件的数量
rename(const Path1& from_p, const Path2&to_p);       重命名
copy_file(const Path1& from_fp, const Path2& to_fp);   拷贝文件
create_directories(const Path &p);                               建立路径

directory_iterator     :只支持本层目录遍历

recursive_directory_iterator :支持深度遍历

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值