Boost--filesystem
頭文件
#include <boost/filesystem.hpp>
所有Boost.Filesystem庫(kù)的內(nèi)容都處于名空間boost::filesystem之內(nèi)。
認(rèn)識(shí)basic_path類(lèi)
在Boost.Filesystem庫(kù)里basic_path是最重要的類(lèi),它以系統(tǒng)無(wú)關(guān)的方式保存路徑、文件名。象std::basic_string 一樣,針對(duì)char和wchar_t,分別特化了path和wpath。
basic_path的構(gòu)造函數(shù):
basic_path( const string_type & s ); basic_path( const value_type * s ); template <class InputIterator> basic_path(InputIterator s, InputIterator last);輸入?yún)?shù)是一個(gè)字符串(或字符迭代器),表示路徑名,可以輸入系統(tǒng)原生路徑名或可移植路徑名
原生路徑名沒(méi)啥好說(shuō)的,比如C:\Windows; D:\abc\ttt.txt等
可移植路徑名的定義和Unix的路徑定義相同,以“/”作為路徑分隔符。
basic_path成員函數(shù):
成員函數(shù) 作用
template <class InputIterator>basic_path& append(InputIterator first, InputIterator last); 將字符串 s 或字符序列 [first,last) 中的路徑元素追加到保存的路徑中。
basic_path& remove_filename(); 去除路徑中的文件名
basic_path& replace_extension( const string_type & new_extension = "" ); 替換擴(kuò)展名
string_type string() 得到可移植路徑名
string_type file_string() 得到系統(tǒng)原生文件名
string_type directory_string() 得到系統(tǒng)原生路徑名
string_type root_name() const; 得到根名
string_type root_directory() const; 得到根目錄
basic_path root_path() const; 得到根路徑:根名+根目錄
basic_path relative_path() const; 得到相對(duì)路徑
string_type filename() const; 得到文件名
basic_path parent_path() const; 得到父路徑:根路徑+相對(duì)路徑
string_type stem(const Path & p) const; 得到不帶擴(kuò)展名的文件名
string_type extension(const Path & p) const; 得到擴(kuò)展名
bool empty() const; path未賦值
bool is_complete() const; 是否是完整路徑
bool has_root_path() const;
bool has_root_name() const;
bool has_root_directory() const;
bool has_relative_path() const;
bool has_filename() const;
bool has_branch_path() const; 路經(jīng)中是否包含指定的項(xiàng)
測(cè)試代碼:
#include "boost/filesystem.hpp" // 包含所有需要的 Boost.Filesystem 聲明
#include <iostream> // 使用 std::cout
namespace fs = boost::filesystem;
// 宏FSTEST:測(cè)試f的成員函數(shù),輸出成員函數(shù)名和結(jié)果
#define FSTEST(x) std::cout << #x##": " << f.x << std::endl
int main()
{
fs::path f("\\folder1\\folder2\\folder3\\filename.ext");
FSTEST(string());
FSTEST(file_string());
FSTEST(directory_string());
FSTEST(root_name());
FSTEST(root_directory());
FSTEST(root_path());
FSTEST(relative_path());
FSTEST(filename());
FSTEST(parent_path());
FSTEST(stem());
FSTEST(extension());
FSTEST(replace_extension("new"));
char buf[]="hello";
FSTEST(append(buf, buf+sizeof(buf)));
FSTEST(remove_filename());
return 0;
}
輸出:
string(): /folder1/folder2/folder3/filename.ext file_string(): \folder1\folder2\folder3\filename.ext directory_string(): \folder1\folder2\folder3\filename.ext root_name(): root_directory(): / root_path(): / relative_path(): folder1/folder2/folder3/filename.ext filename(): filename.ext parent_path(): /folder1/folder2/folder3 stem(): filename extension(): .ext replace_extension("new"): /folder1/folder2/folder3/filename.new append(buf, buf+sizeof(buf)): /folder1/folder2/folder3/filename.new/hello remove_filename(): /folder1/folder2/folder3/filename.new/
常用函數(shù)
函數(shù)名 作用
system_complete(path); 返回完整路徑(相對(duì)路徑+當(dāng)前路徑)
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); 是否是一個(gè)鏈接文件
file_status status(path); 返回路徑名對(duì)應(yīng)的狀態(tài)
template <class Path> const Path& initial_path(); 得到程序運(yùn)行時(shí)的系統(tǒng)當(dāng)前路徑
template <class Path> Path current_path(); 得到系統(tǒng)當(dāng)前路徑
template <class Path> void current_path(const Path& p); 改變當(dāng)前路徑
template <class Path> space_info space(const Path& p); 得到指定路徑下的空間信息,space_info 有capacity, free 和 available三個(gè)成員變量,分別表示容量,剩余空間和可用空間。
template <class Path> std::time_t last_write_time(const Path& p); 最后修改時(shí)間
template <class Path> void last_write_time(const Path& p, const std::time_t new_time); 修改最后修改時(shí)間
template <class Path> bool create_directory(const Path& dp); 建立路徑
template <class Path1, class Path2> void create_hard_link(const Path1& to_p, const Path2& from_p);
template <class Path1, class Path2> error_code create_hard_link(const Path1& to_p,
const Path2& from_p, error_code& ec); 建立硬鏈接
template <class Path1, class Path2> void create_symlink(const Path1& to_p, const Path2& from_p);
template <class Path1, class Path2> error_code create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec); 建立軟鏈接
template <class Path> void remove(const Path& p, system::error_code & ec = singular ); 刪除文件
template <class Path> unsigned long remove_all(const Path& p); 遞歸刪除p中所有內(nèi)容,返回刪除文件的數(shù)量
template <class Path1, class Path2> void rename(const Path1& from_p, const Path2& to_p); 重命名
template <class Path1, class Path2> void copy_file(const Path1& from_fp, const Path2& to_fp); 拷貝文件
template <class Path> Path complete(const Path& p, const Path& base=initial_path<Path>()); 以base以基,p作為相對(duì)路徑,返回其完整路徑
template <class Path> bool create_directories(const Path & p); 建立路徑
路徑迭代器
basic_directory_iterator
構(gòu)造函數(shù):
explicit basic_directory_iterator(const Path& dp); basic_directory_iterator();basic_directory_iterator 從構(gòu)造參數(shù)得到目錄,每一次調(diào)用 operator++,它就查找并得到下一個(gè)文件名直到目錄元素的末尾。不帶參數(shù)的構(gòu)造函數(shù) basic_directory_iterator() 總是構(gòu)造一個(gè) end 迭代器對(duì)象,它是唯一一個(gè)用于結(jié)束條件的合法迭代器。
示例代碼,得到指定目錄下的所有文件名:
void find_file( const fs::path & dir_path )
{
if ( !fs::exists( dir_path ) ) return;
fs::directory_iterator end_itr; // 缺省構(gòu)造生成一個(gè)結(jié)束迭代器
for ( fs::directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( fs::is_directory(itr->status()) )
{
find_file( itr->path() ); //遞歸查找
}
else
{
std::cout << *itr << std::endl;
}
}
}
basic_recursive_directory_iterator
遞歸遍歷目錄的迭代器,它的構(gòu)造參數(shù)與basic_directory_iterator相同,當(dāng)調(diào)用 operator++時(shí),如果當(dāng)前值是一個(gè)目錄,則進(jìn)入下一級(jí)目錄。
它有三個(gè)成員函數(shù): 函數(shù)名 作用
int level() const; 得到當(dāng)前搜索深度
void pop(); 調(diào)用pop()后,下一次遞增就會(huì)直接返回上一級(jí)目錄
void no_push(); 調(diào)用no_push()后,即便下一個(gè)元素是目錄類(lèi)型也不進(jìn)入
示例代碼,得到指定目錄下的所有文件名(和上例作用相同):
void find_file2( const fs::path & dir_path )
{
fs::recursive_directory_iterator end_itr; // 缺省構(gòu)造生成一個(gè)結(jié)束迭代器
for ( fs::recursive_directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
std::cout << itr.level() << *itr << std::endl;
}
轉(zhuǎn)自:http://hi.baidu.com/dulamhoo/blog/item/c501e83a48c2bcf4838b1380.html