boost 文件操作
生活随笔
收集整理的這篇文章主要介紹了
boost 文件操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如果要簡(jiǎn)單處理文件和文件夾的時(shí)候(刪除、重命名等),使用Windows的系統(tǒng)函數(shù)會(huì)十分麻煩,可以嘗試一下使用Boost庫(kù)來進(jìn)行處理
頭文件
#include <boost/filesystem.hpp>如果要獲得每次處理的結(jié)果錯(cuò)誤碼,需要加上頭文件:
#include <boost/system/error_code.hpp> boost::system::error_code err;如果不需要的話只需要把err去掉
以下路徑均為絕對(duì)路徑
基礎(chǔ)類型
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)創(chuàng)建文件/文件夾:
boost::filesystem::create_directories(filepath, err)獲取當(dāng)前路徑:
boost::filesystem::current_path()復(fù)制文件/文件夾:
boost::filesystem::copy(frompath, topath)遍歷文件夾:
boost::filesystem::directory_iterator fileIter(filepath); boost::filesystem::directory_iterator enditer; for (; fileIter != enditer; ++fileIter) {//每個(gè)文件的路徑fileIter->path(); }判斷文件是否存在父目錄
filepath.has_root_directory() //true 存在父目錄 //false 不存在獲取當(dāng)前文件路徑的父目錄
filepath.parent_path()判斷當(dāng)前文件路徑是否是 ‘.’/’…’(遍歷的時(shí)候可能用得上):
filepath.filename_is_dot() filepath.filename_is_dot_dot()如果需要,還可以將文件路徑轉(zhuǎn)成string或wstring
filepath->string() filepath->wstring()總結(jié)
以上是生活随笔為你收集整理的boost 文件操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在隆鼻多少钱啊?
- 下一篇: 新乡治疗女性卵巢多囊最好的医院推荐