如果要簡單處理文件和文件夾的時候(刪除、重命名等),使用Windows的系統函數會十分麻煩,可以嘗試一下使用Boost庫來進行處理
頭文件
#include <boost/filesystem.hpp>
如果要獲得每次處理的結果錯誤碼,需要加上頭文件:
#include <boost/system/error_code.hpp>
boost::system::error_code err;
如果不需要的話只需要把err去掉
以下路徑均為絕對路徑
基礎類型
boost::filesystem::path filepath;
判斷文件/文件夾是否存在:
boost::filesystem::exists(filepath, err)
//true 存在
//false 不存在
判斷文件路徑是否文件夾:
boost::filesystem::is_directory(filepath, err)
//true 是
//false 不是
刪除文件:
boost::filesystem::remove(filepath, err)
//true 刪除成功
//false 刪除失敗
重命名文件:
boost::filesystem::rename(oldname, newname, err)
創建文件/文件夾:
boost::filesystem::create_directories(filepath, err)
獲取當前路徑:
boost::filesystem::current_path()
復制文件/文件夾:
boost::filesystem::copy(frompath, topath)
遍歷文件夾:
boost::filesystem::directory_iterator fileIter(filepath);
boost::filesystem::directory_iterator enditer;
for (; fileIter != enditer; ++fileIter) {//每個文件的路徑fileIter->path();
}
判斷文件是否存在父目錄
filepath.has_root_directory()
//true 存在父目錄
//false 不存在
獲取當前文件路徑的父目錄
filepath.parent_path()
判斷當前文件路徑是否是 ‘.’/’…’(遍歷的時候可能用得上):
filepath.filename_is_dot()
filepath.filename_is_dot_dot()
如果需要,還可以將文件路徑轉成string或wstring
filepath->string()
filepath->wstring()