boost學習之—Filesystem

要點:

1 頭文件

   #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations

   #include <iostream> // for std::cout

   using boost::filesystem;

2 在path.hpp中定義了路徑類

   a 支持多種編碼格式,包括unicode

   b 多種構造函數,可支持多種類型的構造

   c 支持多種路徑格式的寫法,如‘\’與'/'的路徑分隔符問題

   d 支持多種訪問函數,可參考path類源碼

   總之,一句話,該類真的很方便,很強大;

3 在operations.hpp文件中定義了很多關於訪問,操縱文件的一些實用函數

   如; std::cout<<argv[0]<< " " << file_size(argv[0])<< endl;

    path p (argv[0]);

    exists(p);

    is_regular_file(p);

    is_directory(p);

    等等,在使用的時候,打開頭文件查詢便知;

4 directory_iterator提供了目錄的迭代器功能,如:

     path pt (argv[0]);
     path p = pt.parent_path();

      copy(directory_iterator(p), directory_iterator(),      ostream_iterator<directory_entry>(cout, "\n"));//輸出當前運行目錄中的文件及目錄

     directory_entry裏面記錄了路徑及status信息,代表迭代的對象;

     並且與標準庫兼容,如:

       typedef vector<path> vec;             // store paths,
        vec v;                                // so we can sort them later

       copy(directory_iterator(p), directory_iterator(), back_inserter(v));
                sort(v.begin(), v.end());  

        for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
        {
          cout << "   " << *it << '\n';
        }

5 path的例子,看例子學習,一切盡在不言中!

     for (; argc > 1; --argc, ++argv)
          p /= argv[1];//利用operator/來

  cout  <<  "\ncomposed path:\n";
  cout  <<  "  cout << -------------: " << p << "\n";
  cout  <<  "  make_preferred()----------: " << path(p).make_preferred() << "\n";

  cout << "\nelements:\n";

  for (path::iterator it(p.begin()), it_end(p.end()); it != it_end; ++it)
    cout << "  " << *it << '\n';

  cout  <<  "\nobservers, native format:" << endl;
# ifdef BOOST_POSIX_API
  cout  <<  "  native()-------------: " << p.native() << endl;
  cout  <<  "  c_str()--------------: " << p.c_str() << endl;
# else  // BOOST_WINDOWS_API
  wcout << L"  native()-------------: " << p.native() << endl;
  wcout << L"  c_str()--------------: " << p.c_str() << endl;
# endif
  cout  <<  "  string()-------------: " << p.string() << endl;
  wcout << L"  wstring()------------: " << p.wstring() << endl;

  cout  <<  "\nobservers, generic format:\n";
  cout  <<  "  generic_string()-----: " << p.generic_string() << endl;
  wcout << L"  generic_wstring()----: " << p.generic_wstring() << endl;

  cout  <<  "\ndecomposition:\n";
  cout  <<  "  root_name()----------: " << p.root_name() << '\n';
  cout  <<  "  root_directory()-----: " << p.root_directory() << '\n';
  cout  <<  "  root_path()----------: " << p.root_path() << '\n';
  cout  <<  "  relative_path()------: " << p.relative_path() << '\n';
  cout  <<  "  parent_path()--------: " << p.parent_path() << '\n';
  cout  <<  "  filename()-----------: " << p.filename() << '\n';
  cout  <<  "  stem()---------------: " << p.stem() << '\n';
  cout  <<  "  extension()----------: " << p.extension() << '\n';

 6 file stream

   從標準庫的file stream中繼承實現boost的類,增加以path作爲函數參數的函數;


總結,本文僅簡單的介紹了boost庫的Filesystem庫的基本使用方法,更詳細的使用請參考源碼和相關文檔;該庫在實際當中是非常有用的;




    


發佈了70 篇原創文章 · 獲贊 8 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章