diff --git a/CPP17.md b/CPP17.md index 644733c..1c81c6e 100644 --- a/CPP17.md +++ b/CPP17.md @@ -26,6 +26,7 @@ C++17 includes the following new library features: - [std::invoke](#stdinvoke) - [std::apply](#stdapply) - [splicing for maps and sets](#splicing-for-maps-and-sets) +- [std::filesystem](#stdfilesystem) ## C++17 Language Features @@ -365,6 +366,22 @@ m.insert(std::move(e)); // m == { { 1, "one" }, { 3, "three" }, { 4, "two" } } ``` +### std::filesystem +The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. + +Here, a big file is copied to a temporary path if there is available space: +```c++ +const auto bigFilePath {"bigFileToCopy"}; +if (std::filesystem::exists(bigFilePath)) { + const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; + std::filesystem::path tmpPath {"/tmp"}; + if (std::filesystem::space(tmpPath).available > bigFileSize) { + std::filesystem::create_directory(tmpPath.append("example")); + std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); + } +} +``` + ## Acknowledgements * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. diff --git a/README.md b/README.md index dffc0ad..b584c57 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ C++17 includes the following new library features: - [std::invoke](#stdinvoke) - [std::apply](#stdapply) - [splicing for maps and sets](#splicing-for-maps-and-sets) +- [std::filesystem](#stdfilesystem) C++14 includes the following new language features: - [binary literals](#binary-literals) @@ -423,6 +424,21 @@ e.key() = 4; m.insert(std::move(e)); // m == { { 1, "one" }, { 3, "three" }, { 4, "two" } } ``` +### std::filesystem +The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. + +Here, a big file is copied to a temporary path if there is available space: +```c++ +const auto bigFilePath {"bigFileToCopy"}; +if (std::filesystem::exists(bigFilePath)) { + const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; + std::filesystem::path tmpPath {"/tmp"}; + if (std::filesystem::space(tmpPath).available > bigFileSize) { + std::filesystem::create_directory(tmpPath.append("example")); + std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); + } +} +``` ## C++14 Language Features