天天看點

Boost學習之可移植路徑操作--filesystem

Boost.Filesystem 庫為對路徑、檔案和目錄進行查詢和操作提供了可移植的工具,已經被C++标準委員會接納包含到TR2中。

使用Boost.Filesystem 庫之前要先編譯它,請參考《Boost的編譯》

所有Boost.Filesystem庫的内容都處于名空間boost::filesystem之内。

在Boost.Filesystem庫裡basic_path是最重要的類,它以系統無關的方式儲存路徑、檔案名。象std::basic_string 一樣,針對char和wchar_t,分别特化了path和wpath。

basic_path的構造函數:

輸入參數是一個字元串(或字元疊代器),表示路徑名,可以輸入系統原生路徑名或可移植路徑名

原生路徑名沒啥好說的,比如C:\Windows; D:\abc\ttt.txt等

可移植路徑名的定義和Unix的路徑定義相同,以“/”作為路徑分隔符。

成員函數

作用

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 = "" );

替換擴充名

string_type string()

得到可移植路徑名

string_type file_string()

得到系統原生檔案名

string_type directory_string()

得到系統原生路徑名

string_type root_name() const;

得到根名

string_type root_directory() const;

得到根目錄

basic_path root_path() const;

得到根路徑:根名+根目錄

basic_path relative_path() const;

得到相對路徑

string_type filename() const;

得到檔案名

basic_path parent_path() const;

得到父路徑:根路徑+相對路徑

string_type stem(const Path & p) const;

得到不帶擴充名的檔案名

string_type extension(const Path & p) const;

得到擴充名

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;

路經中是否包含指定的項

#include "boost/filesystem.hpp"   // 包含所有需要的 Boost.Filesystem 聲明

#include <iostream>               // 使用 std::cout

namespace fs = boost::filesystem;

// 宏FSTEST:測試f的成員函數,輸出成員函數名和結果

#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;

}

函數名

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);

傳回路徑名對應的狀态

template <class Path> const Path& initial_path();

得到程式運作時的系統目前路徑

template <class Path> Path current_path();

得到系統目前路徑

template <class Path> void current_path(const Path& p);

改變目前路徑

template <class Path> space_info space(const Path& p);

得到指定路徑下的空間資訊,space_info 有capacity, free 和 available三個成員變量,分别表示容量,剩餘空間和可用空間。

template <class Path> std::time_t last_write_time(const Path& p);

最後修改時間

template <class Path> void last_write_time(const Path& p, const std::time_t new_time);

修改最後修改時間

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中所有内容,傳回删除檔案的數量

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作為相對路徑,傳回其完整路徑

template <class Path> bool create_directories(const Path & p);

構造函數:

<code><code></code></code>

<code>basic_directory_iterator</code> 從構造參數得到目錄,每一次調用 <code>operator++</code>,它就查找并得到下一個檔案名直到目錄元素的末尾。不帶參數的構造函數 <code>basic_directory_iterator()</code> 總是構造一個 end 疊代器對象,它是唯一一個用于結束條件的合法疊代器。

示例代碼,得到指定目錄下的所有檔案名:

void find_file( const fs::path &amp; dir_path )

    if ( !fs::exists( dir_path ) ) return;

    fs::directory_iterator end_itr; // 預設構造生成一個結束疊代器

    for ( fs::directory_iterator itr( dir_path );

        itr != end_itr;

        ++itr )

    {

        if ( fs::is_directory(itr-&gt;status()) )

        {

            find_file( itr-&gt;path() ); //遞歸查找

        }

        else

            std::cout &lt;&lt; *itr &lt;&lt; std::endl;

  }

遞歸周遊目錄的疊代器,它的構造參數與basic_directory_iterator相同,當調用 <code>operator++</code>時,如果目前值是一個目錄,則進入下一級目錄。

它有三個成員函數:

int level() const;

得到目前搜尋深度

void pop();

調用pop()後,下一次遞增就會直接傳回上一級目錄

void no_push();

調用no_push()後,即便下一個元素是目錄類型也不進入

示例代碼,得到指定目錄下的所有檔案名(和上例作用相同):

void find_file2( const fs::path &amp; dir_path )

    fs::recursive_directory_iterator end_itr; // 預設構造生成一個結束疊代器

    for ( fs::recursive_directory_iterator itr( dir_path );

        std::cout &lt;&lt; itr.level() &lt;&lt; *itr &lt;&lt; std::endl;

    }