當前位置:
首頁 >
boost 文件操作
發布時間:2023/12/1
30
豆豆
默认站点
收集整理的這篇文章主要介紹了
boost 文件操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如果要簡單處理文件和文件夾的時候(刪除、重命名等),使用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()總結
以上是默认站点為你收集整理的boost 文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在隆鼻多少钱啊?
- 下一篇: Qt异常结束程序无法重新运行