(九)boost库之文件处理filesystem
filesystem庫是一個可移植的文件系統操作庫,它在底層做了大量的工作,使用POSIX標準表示文件系統的路徑,使C++具有了類似腳本語言的功能,可以跨平臺操作目錄、文件,寫出通用的腳本程序。
1.path的構造函數可以接受C字符串和string,也可以是一個指定首末迭代器字符串序列區間。
2.filesystem提供了一系列的文件名(或目錄)檢查函數。
3.有豐富的函數用于獲取文件名、目錄名、判斷文件屬性等等。
4.filesystem庫使用異常來處理文件操作時發生的錯誤。
5.filesystem庫提供一個文件狀態類file_status及一組相關函數,用于檢查文件的各種屬性,如是否存在、是否是目錄、是否是符號鏈接等。
6.filesystem提供了少量的文件屬性操作,如windows下的只讀、歸檔等,Linux下的讀寫權限等。
7.文件操作,如創建目錄、文件改名、文件刪除、文件拷貝等等。
8.basic_directory_iterator提供了迭代一個目錄下所有文件的功能。
一、path類的基本用法
//注意 /= 和 += 的區別, /= 表示追加下級目錄, += 僅僅是字符串的串接 path dir("C:\\Windows"); dir /= "System32"; //追加下級目錄 dir /= "services.exe"; std::cout << dir << std::endl; std::cout << dir.string() << std::endl; //轉換成std::string 類型 std::cout << dir.root_name()<< std::endl; //盤符名:C: std::cout << dir.root_directory()<< std::endl; //根目錄:"\" std::cout << dir.root_path()<< std::endl; //根路徑:"C:\" std::cout << dir.relative_path()<< std::endl; // 相對路徑:Windows\System32\services.exe std::cout << dir.parent_path()<< std::endl; //上級目錄:C:\Windows\System32 std::cout << dir.filename()<< std::endl; //文件名:services.exe std::cout << dir.stem()<< std::endl; //不帶擴展的文件名:services std::cout << dir.extension()<< std::endl; //擴展名:.exe二、常用函數及異常處理
函數名?????????????????????????????????????????? 作用
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);??????????????????? 拷貝文件?
omplete(const Path& p, const Path& base=initial_path<Path>()); 以base以基,p作為相對路徑,返回其完整路徑?
create_directories(const Path & p);?????????????????????????????????????????????????????? 建立路徑
三、目錄迭代
boost庫提供了兩個迭代器
directory_iterator :只支持本層目錄遍歷recursive_directory_iterator :支持深度遍歷
輸出指定目錄下的文件:
path dir2("c:\\Windows\\System32"); directory_iterator end; for (directory_iterator pos(dir2); pos != end; pos++) { std::cout << *pos << std::endl; }使用深度遍歷
typedef recursive_directory_iterator rd_iterator; path dir2("E:\\Student"); rd_iterator end; for (rd_iterator pos(dir); pos != end; pos++) { //如果深度大于4層,則不再繼續深入 if (is_directory(*pos) && pos.level() > 4) { pos.no_push(); } //如果該目錄下有nofind.txt文件,則跳出該目錄 if (*pos == "nofind.txt") { pos.pop(); } }?
四、示例
找出指定目錄及子目錄下匹配的文件
#include <iostream> #include <boost/filesystem.hpp> #include <boost/xpressive/xpressive_dynamic.hpp> #include <boost/algorithm/string.hpp> #include <vector> using namespace boost::filesystem; using namespace boost::xpressive; typedef recursive_directory_iterator rd_iterator; //獲取過濾的文件 void GetFilterFile(const std::string& filter,std::vector<path>& vecFile) { path filterPath = dir / filter; path filterDir1 = filterPath.parent_path(); std::string filename = filterPath.filename().string(); //轉換文件通配符為正則表達式 std::string str = boost::replace_all_copy(boost::replace_all_copy(filename, ".", "\\."), "*", ".*"); sregex reg = sregex::compile(str); if (!exists(filterDir1) || !is_directory(filterDir1)) { return; } //查找當前目錄及子目錄的所有文件,如果符合正則表達式則添加到列表中 rd_iterator end; for (rd_iterator pos(filterDir1); pos != end; pos++) { if (!is_directory(*pos) && regex_match(pos->path().filename().string(), reg)) { vecFile.push_back(pos->path()); } } }總結
以上是生活随笔為你收集整理的(九)boost库之文件处理filesystem的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (八)boost库之异常处理
- 下一篇: (十)boost库之多线程