std::filesystem::copy 複製檔案的用法與範例

本篇介紹 C++ 的 std::filesystem::copy 用法,並使用 std::filesystem::copy 來複製檔案。

要使用 std::filesystem::copy 複製檔案的話,需要引入的標頭檔: <filesystem>
各家編譯器還沒正式支援 C++17 前, 可能需要引入 <experimental/filesystem> 標頭檔
引入<filesystem>標頭檔是使用 std::filesystem::copy
引入<experimental/filesystem>標頭檔是使用 std::experimental::filesystem::copy
GCC:g++ 大概要 8 以上才有正式支援 C++17,編譯時要加入-lstdc++fs選項。
Clang:Clang 5.0 應該已經支援。
Visual Studio: Visual Studio 2017 15.3 還是只能使用 std::experimental namespace。

基本使用範例

預設複製目錄時不會遞迴式的複製,也就是只有目錄下的檔案會被複製,目錄下的子目錄不會被複製。
若要連目錄下的子目錄一起複製的話可以使用 copy_options::recursive 這個參數。

std-filesystem-copy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// g++ std-filesystem-copy.cpp -o a.out -std=c++11 -lstdc++fs
#include <iostream>
#include <experimental/filesystem>
//#include <filesystem> // C++17

int main()
{
// The default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories
std::experimental::filesystem::copy("dir1", "dir2");

// While with copy_options::recursive, the subdirectories are also copied, with their content, recursively.
std::experimental::filesystem::copy("dir1", "dir3", std::experimental::filesystem::copy_options::recursive);

// C++17
//std::filesystem::copy("dir1", "dir2");
//std::filesystem::copy("dir1", "dir3", std::filesystem::copy_options::recursive);
}

範例. 處理 C++17 以及尚未支援 C++17 的方式

若想要同時支援新舊版寫法或者是不同平台編譯器的支援程度不相同時,
可以考慮使用 CXX17 這個定義,用這個定義去需分是不是正式支援C++17,
順便在使用 namespace 的方式讓兩種寫法一致化,如下列範例所示:

std-filesystem-copy2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// g++ std-filesystem-copy2.cpp -o a.out -std=c++11 -lstdc++fs
#include <iostream>
#ifdef CXX17 // if this is C++17
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

int main()
{
// The default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories
fs::copy("dir1", "dir2");

// While with copy_options::recursive, the subdirectories are also copied, with their content, recursively.
fs::copy("dir1", "dir3", std::experimental::filesystem::copy_options::recursive);
}

如果來源檔 dir1 不存在會怎樣?
會發生core dumped,如下列輸出:

1
2
3
terminate called after throwing an instance of 'std::experimental::filesystem::v1::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: No such file or directory [dir1] [dir2]
Aborted (core dumped)

參考
std::filesystem::copy - cppreference.com
https://en.cppreference.com/w/cpp/filesystem/copy
c++11 - Are the experimental features of modern C++ reliable for long-term projects? - Stack Overflow
https://stackoverflow.com/questions/43318493/are-the-experimental-features-of-modern-c-reliable-for-long-term-projects
c++ - fatal error: filesystem: No such file or directory - Stack Overflow
https://stackoverflow.com/questions/39231363/fatal-error-filesystem-no-such-file-or-directory/39231488
C++17 Filesystem
https://www.codingame.com/playgrounds/5659/c17-filesystem

其它相關文章推薦
C/C++ 新手入門教學懶人包
std::vector 用法與範例
std::queue 用法與範例
std::deque 用法與範例
std::thread 用法與範例
std::mutex 用法與範例
std::condition_variable 用法與範例