C++遍历文件或文件夹
我们常常需要遍历一个文件夹下的所有文件或文件夹。
boost使用wrecursive_directory_iterator提供了遍历子目录或子目录下的文件的能力,使用非常方便。
<span style="font-family:Courier New;font-size:14px;">#include <vector> #include <iostream> #include <string> #include <algorithm> #include <iterator> #include <iosfwd> #include <tchar.h> #include "boost/timer.hpp" #include "boost/filesystem.hpp" #include "boost/algorithm/string.hpp" using namespace std; void GetFilenameByPath(vector<wstring> &_files_name_vec, boost::filesystem::wpath _path, wstring _ext_str = _T("")); void GetFilePathByPath(vector<wstring> &_files_name_vec, boost::filesystem::wpath _path); int main() { boost::timer tm; vector<wstring> files_vec; wcout.imbue(locale("chs")); wcout << _T("遍历文件夹:") << endl; GetFilePathByPath(files_vec, _T("D:\KuGou")); copy(files_vec.begin(), files_vec.end(), ostream_iterator<wstring, wchar_t>(wcout, _T(" "))); wcout << _T("paths:") << files_vec.size() << _T("个 use time:") << tm.elapsed() << _T("S") << endl; tm.restart(); files_vec.clear(); files_vec.swap(files_vec); wcout <<endl<< _T("遍历文件:") << endl; GetFilenameByPath(files_vec, _T("D:\KuGou")); copy(files_vec.begin(), files_vec.end(), ostream_iterator<wstring, wchar_t>(wcout, _T(" "))); wcout << _T("files:") << files_vec.size() << _T("个 use time:") << tm.elapsed()<<_T("S") << endl; system("pause"); return 0; } void GetFilenameByPath(vector<wstring> &_files_name_vec, boost::filesystem::wpath _path, wstring _ext_str) { boost::filesystem::wrecursive_directory_iterator itr_end; for (boost::filesystem::wrecursive_directory_iterator itr_beg(_path); itr_beg != itr_end; ++itr_beg) { if ((_ext_str == _T("") || boost::ends_with(itr_beg->path().wstring(), _ext_str)) && boost::filesystem::is_regular_file(*itr_beg)) { _files_name_vec.push_back(itr_beg->path().wstring()); } } } void GetFilePathByPath(vector<wstring> &_files_name_vec, boost::filesystem::wpath _path) { boost::filesystem::wrecursive_directory_iterator itr_end; for (boost::filesystem::wrecursive_directory_iterator itr_beg(_path); itr_beg != itr_end; ++itr_beg) { if (boost::filesystem::is_directory(*itr_beg)) { _files_name_vec.push_back(itr_beg->path().wstring()); } } }</span>
运行截图:
注意:filesystem库需要编译(不编译直接包含文件也可以),并且它还依赖system库,
1、文件操作:
filesystem库基于path的路径表示提供了基本的文件操作函数,如创建目录
create_directory、文件改名rename、文件删除remove、文件拷贝copy_file等。。
remove只能一次删除一个目录或文件,remove_all可以递归删除。
create_directory只能创建一级目录,create_directories可以一次创建多级目录。
2、文件属性:
函数initial_path()返回程序启动时(进行main()函数)的当前路径
函数current_path()返回当前路径。它和initial_path()返回的都是一个完整路径(绝
对路径);
函数file_size()以字节为单位返回文件的大小;
函数last_write_time()文件的最后修改时间,是一个std::time_t。
last_write_time()还可以额外接受一个time_t参数,修改文件的最后修改时间。就像是
使用linux下的touch命令。
这些函数都要求操作的文件必须存在,否则会抛出异常,file_size()还要求文件必须是
个普通文件is_regular_file(name) == true
此外,函数space()可以返回一个space_info结构,它表明了该路径下的磁盘空间分配
情况
3、路径处理
path类提供了丰富的函数用于处理路径,可以获取文件名、目录名、判断文件属性等等,
path的成员函数string()返回标准格式的路径表示,directory_string()返回文件系统
格式的路径表示,parent_path()、stem()、filename()和extension()分别返回路径
中的父路径、不含扩展名的全路径名、文件名和扩展名。
is_complete()用于检测path是否是一个完整(绝对)路径,这需要依据具体的文件系统
的表示,例如在windows系统的完整路径需要包括盘符。
root_name()、root_directory()、root_path()这三个成员函数用于处理根目录,如果
path中含有根,那么它们分别返回根的名字、根目录和根路径。
relative_path()返回path的相对路径,相当于去掉了root_path()。
根路径的相对路径的这四个函数都有对应的has_xxx()的形式,用来判断是否存在对应的
路径。同样,has_filename()和has_parent_path()用于判断路径是否有文件名或者父
路径。
- 上一篇: C/C++常用库及工具
- 下一篇: 浅谈文字编码和Unicode