std::experimental::filesystem::copy
來自 cppreference.com
< cpp | experimental | fs
定義於標頭檔案 <experimental/filesystem> |
||
void copy( const path& from, const path& to ); void copy( const path& from, const path& to, error_code& ec ); |
(1) | (filesystem TS) |
void copy( const path& from, const path& to, copy_options options ); void copy( const path& from, const path& to, copy_options options, error_code& ec ); |
(2) | (filesystem TS) |
複製檔案和目錄,提供多種選項
1) 預設行為,等價於使用
copy_options::none
作為 options 的 (2)。2) 將檔案或目錄 from 複製到檔案或目錄 to,使用 options 指定的複製選項。如果 options 中存在 copy_options 選項組中的多個選項(即使是與
copy
無關的 copy_file
組中的選項),則行為未定義。行為如下
- 首先,在執行任何其他操作之前,透過不多於一次對 status 的呼叫(或者,如果 options 中存在
copy_options::skip_symlinks
或copy_options::create_symlinks
,則透過對symlink_status
的呼叫)獲取 from 的型別和許可權。 - 如有必要,以相同方式透過不多於一次 status 或 symlink_status 呼叫獲取 to 的狀態。
- 如果 from 不存在,則報告錯誤。
- 如果 from 和 to 是透過 equivalent() 確定的同一檔案,則報告錯誤。
- 如果 from 或 to 不是常規檔案、目錄或符號連結(由 is_other 確定),則報告錯誤。
- 如果 from 是目錄,但 to 是常規檔案,則報告錯誤。
- 如果 from 是符號連結,則
- 如果 options 中存在
copy_options::skip_symlink
,則不執行任何操作。 - 否則,如果 to 不存在且 options 中存在
copy_options::copy_symlinks
,則行為等價於 copy_symlink(from, to)。 - 否則,報告錯誤。
- 如果 options 中存在
- 否則,如果 from 是常規檔案,則
- 如果 options 中存在
copy_options::directories_only
,則不執行任何操作。 - 否則,如果 options 中存在
copy_options::create_symlinks
,則建立指向 to 的符號連結。注意:除非 to 在當前目錄中,否則 from 必須是絕對路徑。 - 否則,如果 options 中存在
copy_options::create_hard_links
,則建立指向 to 的硬連結。 - 否則,如果 to 是目錄,則行為等價於 copy_file(from, to/from.filename(), options)(將 from 的副本作為檔案建立在目錄 to 中)。
- 否則,行為等價於 copy_file(from, to, options)(複製檔案)。
- 如果 options 中存在
- 否則,如果 from 是目錄且 options 具有
copy_options::recursive
或為copy_options::none
。
- 如果 to 不存在,則首先執行 create_directory(to, from)(建立新目錄並複製舊目錄的屬性)。
- 然後,無論 to 是否已存在或剛剛建立,都透過 for (const directory_entry& x : directory_iterator(from)) 遍歷 from 中包含的檔案,對於每個目錄條目,遞迴呼叫 copy(x.path(), to/x.path().filename(), options | unspecified),其中 unspecified 是一個特殊的位,當在 options 中設定時沒有其他效果(設定此位的唯一目的是防止當 options 為
copy_options::none
時遞迴複製子目錄)。
- 否則不執行任何操作。
目錄 |
[編輯] 引數
from | - | 原始檔、目錄或符號連結的路徑 |
to | - | 目標檔案、目錄或符號連結的路徑 |
ec | - | 非丟擲過載中用於錯誤報告的出參 |
[編輯] 返回值
(無)
[編輯] 異常
不帶 error_code& 引數的過載在底層 OS API 錯誤時丟擲 filesystem_error,構造時以 from 作為第一個引數,to 作為第二個引數,OS 錯誤碼作為錯誤碼引數。如果記憶體分配失敗,可能會丟擲 std::bad_alloc。帶 error_code& 引數的過載在 OS API 呼叫失敗時將其設定為 OS API 錯誤碼,並且在沒有錯誤發生時執行 ec.clear()。此過載具有noexcept 規範:
noexcept
[編輯] 注意
複製目錄時的預設行為是非遞迴複製:檔案被複製,但不包括子目錄
// Given // /dir1 contains /dir1/file1, /dir1/file2, /dir1/dir2 // and /dir1/dir2 contains /dir1/dir2/file3 // After std::experimental::filesystem::copy("/dir1", "/dir3"); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2
而使用 copy_options::recursive
時,子目錄及其內容也會被遞迴複製。
// ...but after std::experimental::filesystem::copy("/dir1", "/dir3", copy_options::recursive); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2 // /dir3/dir2 is created (with the attributes of /dir1/dir2) // /dir1/dir2/file3 is copied to /dir3/dir2/file3
[編輯] 示例
執行此程式碼
#include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::filesystem; int main() { fs::create_directories("sandbox/dir/subdir"); std::ofstream("sandbox/file1.txt").put('a'); fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); // copy file fs::copy("sandbox/dir", "sandbox/dir2"); // copy directory (non-recursive) // sandbox holds 2 files and 2 directories, one of which has a subdirectory // sandbox/file1.txt // sandbox/file2.txt // sandbox/dir2 // sandbox/dir // sandbox/dir/subdir fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive); // sandbox/copy holds copies of the above files and subdirectories fs::remove_all("sandbox"); }
[編輯] 參閱
指定複製操作的語義 (列舉) | |
複製一個符號連結 (函式) | |
複製檔案內容 (函式) |