天天看點

C++17實作檔案操作<filesystem>

文章目錄

  • ​​1、簡介​​
  • ​​1.1 C/C++标準種類​​
  • ​​1.2 VS對C++标準的支援情況​​
  • ​​1.3 gcc對C++标準的支援情況​​
  • ​​2、頭檔案<filesystem>介紹​​
  • ​​2.1 類(Classes)​​
  • ​​2.2 結構(Structs)​​
  • ​​2.3 函數(Functions)​​
  • ​​2.4 操作符(Operators)​​
  • ​​2.5 枚舉(Enumerations)​​
  • ​​3、頭檔案<filesystem>的函數​​
  • ​​4、頭檔案<filesystem>的path類​​
  • ​​5、to_string函數​​
  • ​​結語​​

1、簡介

<filesystem>是用于通路操作和檢索有關路徑、檔案和目錄的資訊的類和函數的頭檔案。

在 Visual Studio 2017 釋出時,<filesystem>頭檔案還不是 C++ 标準。Visual Studio 2017 RTW 中的 C++ 實作了ISO/IEC JTC 1/SC 22/WG 21 N4100中的最終草案标準。Visual Studio 2017 版本 15.7 及更高版本支援新的 C++17<filesystem>标準。這是一個全新的實作,與以前的std::experimental版本不相容。符号連結支援、錯誤修複和标準所需行為的更改使其成為必要。在 Visual Studio 2019 版本 16.3 及更高版本中,包括<filesystem>僅提供新的std::filesystem. 包括<experimental/filesystem>僅提供舊的實驗性實作。實驗性實作将在庫的下一個 ABI 破壞版本中删除。

1.1 C/C++标準種類

  • ⽬前C語⾔的标準有:C89(ANSI C)、C90、C95、C99(ISO C)、C11(C1x)
  • ⽬前C++語⾔的标準有:C++98、C++03(對98⼩幅修改)、C++11(全⾯進化)、C++14、C++17

1.2 VS對C++标準的支援情況

  • C++17

    VS2017基本支援,VS2015部分支援。

  • C++14

    VS2017可以完全支援,VS2015基本支援,VS2013部分支援。

  • C++11

    VS2015及以上完全支援。VS2013基本支援,VS2012部分支援,VS2010及以下版本不支援。

  • C++17實作檔案操作<filesystem>
#ifdef __cpp_lib_filesystem
#pragma  message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma  message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
namespace std {
    namespace filesystem = experimental::filesystem;
}
#endif      
<< __cplusplus << std::endl;      

1.3 gcc對C++标準的支援情況

C++17實作檔案操作<filesystem>

2、頭檔案介紹

​​https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-functions?view=msvc-170​​

2.1 類(Classes)

Name Description
directory_entry class Describes an object that is returned by a directory_iterator or a recursive_directory_iterator and contains a path.
directory_iterator class Describes an input iterator that sequences through the file names in a file-system directory.
filesystem_error class A base class for exceptions that are thrown to report a low-level system overflow.
path class Defines a class that stores an object of template type String that is suitable for use as a file name.
recursive_directory_iterator class Describes an input iterator that sequences through the file names in a file-system directory. The iterator can also descend into subdirectories.
file_status class Wraps a file_type.

2.2 結構(Structs)

Name Description
space_info structure Holds information about a volume.
struct space_info
{
    uintmax_t capacity;
    uintmax_t free;
    uintmax_t available;
};      

2.3 函數(Functions)

​​https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-functions?view=msvc-170​​

2.4 操作符(Operators)

​​https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-operators?view=msvc-170​​

Name Description
operator== bool operator==(const path& left, const path& right) noexcept;
operator!= bool operator!=(const path& left, const path& right) noexcept;
operator< bool operator<(const path& left, const path& right) noexcept;
operator<= bool operator<=(const path& left, const path& right) noexcept;
operator> bool operator>(const path& left, const path& right) noexcept;
operator>= bool operator>=(const path& left, const path& right) noexcept;
operator/ path operator/(const path& left, const path& right);
operator<< template <class Elem, class Traits> basic_ostream<Elem, Traits>& operator<<(basic_ostream<Elem, Traits>& os, const path& pval);
operator>> template <class Elem, class Traits> basic_istream<Elem, Traits>& operator<<(basic_istream<Elem, Traits>& is, const path& pval);

2.5 枚舉(Enumerations)

Name Description
copy_options An enumeration that is used with copy_file and determines behavior if a destination file already exists.
directory_options An enumeration that specifies options for directory iterators.
file_type An enumeration for file types.
perm_options Enumerates options for the permissions function.
perms A bitmask type used to convey permissions and options to permissions

3、頭檔案的函數

  • <filesystem>常用的成員函數如下:
函數名 功能
void copy(const path& from, const path& to) 目錄複制
path absolute(const path& pval, const path& base = current_path()) 擷取相對于base的絕對路徑
bool create_directory(const path& pval) 當目錄不存在時建立目錄
bool create_directories(const path& pval) 形如/a/b/c這樣的,如果都不存在,建立目錄結構
uintmax_t file_size(const path& pval) 傳回目錄的大小
file_time_type last_write_time(const path& pval) 傳回目錄最後修改日期的file_time_type對象
bool exists(const path& pval) 用于判斷path是否存在
bool remove(const path& pval) 删除目錄
uintmax_t remove_all(const path& pval) 遞歸删除目錄下所有檔案,傳回被成功删除的檔案個數
void rename(const path& from, const path& to) 移動檔案或者重命名
#include <iostream>

#ifdef __cpp_lib_filesystem
#pragma  message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma  message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
#endif
namespace fs = std::filesystem;

int main() {
  std::cout << __cplusplus << std::endl;

  fs::current_path(fs::temp_directory_path());
  fs::create_directories("test2022/1/2/a");
  fs::create_directory("test2022/1/2/b");
  fs::permissions("test2022/1/2/b", fs::perms::others_all, fs::perm_options::remove);
  fs::create_directory("test2022/1/2/c", "test2022/1/2/b");

  std::system("tree test2022");
  fs::remove_all("test2022");
  return 0;
}      

4、頭檔案的path類

  • path類常用的成員函數如下:
函數名 功能
path& append(const _Src& source) 在path末尾加入一層結構
path& assign(string_type& source) 指派(字元串)
void clear() 清空
int compare(const path& other) 進行比較
bool empty() 空判斷
path filename() 傳回檔案名(有字尾)
path stem() 傳回檔案名(不含字尾)
path extension() 傳回檔案字尾名
path is_absolute() 判斷是否為絕對路徑
path is_relative() 判斷是否為相對路徑
path relative_path() 傳回相對路徑
path parent_path() 傳回父路徑
path& replace_extension(const path& replace) 替換檔案字尾
#include <iostream>
#include <set>
#ifdef __cpp_lib_filesystem
#pragma  message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma  message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
#endif
namespace fs = std::filesystem;

int main() {
  std::cout << __cplusplus << std::endl;

  fs::path src_dir("D:\\test");
  std::set<string> dir_set;
  for (fs::directory_iterator end, ite(src_dir); ite != end; ++ite)
  {
    if (!fs::is_directory(ite->path()))
      dir_set.insert(ite->path().filename().string());
  };

  return 0;
}      

5、to_string函數

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val)      

結語

繼續閱讀