std::filesystem::copy_file
來自 cppreference.com
< cpp | filesystem
定義於標頭檔案 <filesystem> |
||
bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to ); |
(1) | (C++17 起) |
bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to, |
(2) | (C++17 起) |
bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to, |
(3) | (C++17 起) |
bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to, |
(4) | (C++17 起) |
1,2) 預設值,等價於使用 `copy_options::none` 作為 `options` 的 (3,4)。
3,4) 使用 `options` 中指示的複製選項,將單個檔案從 `from` 複製到 `to`。如果在 `options` 中存在任何 copy_options 選項組中的多個選項(即使是與 filesystem::copy_file 不相關的組),則行為未定義。
- 如果 !filesystem::is_regular_file(from)(原因可能是原始檔不存在或不是常規檔案),則報告錯誤。
- 否則,如果目標檔案不存在,
- 則將 `from` 解析到的檔案的內容和屬性複製到 `to` 解析到的檔案(遵循符號連結)。
- 否則,如果目標檔案已存在,
- 如果以下任何一項為真,則報告錯誤:
- 由 filesystem::equivalent(from, to) 確定 `to` 和 `from` 相同;
- 由 !filesystem::is_regular_file(to) 確定 `to` 不是常規檔案;
- `options` 中未設定任何 filesystem::copy_file 控制選項。
- 否則,如果 `options` 中設定了 `copy_options::skip_existing`,則不執行任何操作。
- 否則,如果 `options` 中設定了 `copy_options::overwrite_existing`,則將 `from` 解析到的檔案的內容和屬性複製到 `to` 解析到的檔案。
- 否則,如果 `options` 中設定了 `copy_options::update_existing`,則僅當 `from` 比 `to` 新時才複製檔案,由 filesystem::last_write_time() 定義。
非丟擲過載在發生錯誤時返回 false。
目錄 |
[編輯] 引數
from | - | 原始檔的路徑 |
to | - | 目標檔案的路徑 |
ec | - | 非丟擲過載中用於錯誤報告的出參 |
[編輯] 返回值
如果檔案被複制,則為 true,否則為 false。
[編輯] 異常
任何未標記為 noexcept
的過載都可能在記憶體分配失敗時丟擲 std::bad_alloc。
1,3) 在底層作業系統 API 錯誤時丟擲 std::filesystem::filesystem_error,構造時 `from` 作為第一個路徑引數,`to` 作為第二個路徑引數,作業系統錯誤碼作為錯誤碼引數。
[編輯] 注意
這些函式最多包含一次對 filesystem::status(to) 的直接或間接呼叫(用於確定檔案是否存在,以及對於 `filesystem::copy_options::update_existing` 選項,用於其最後寫入時間)。
當使用 filesystem::copy_file 複製目錄時會報告錯誤:請使用 filesystem::copy。
filesystem::copy_file 遵循符號連結:請使用 filesystem::copy_symlink 或帶 `filesystem::copy_options::copy_symlinks` 的 filesystem::copy。
[編輯] 示例
執行此程式碼
#include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; int main() { fs::create_directory("sandbox"); std::ofstream("sandbox/file1.txt").put('a'); fs::copy_file("sandbox/file1.txt", "sandbox/file2.txt"); // now there are two files in sandbox: std::cout << "file1.txt holds: " << std::ifstream("sandbox/file1.txt").rdbuf() << '\n'; std::cout << "file2.txt holds: " << std::ifstream("sandbox/file2.txt").rdbuf() << '\n'; // fail to copy directory fs::create_directory("sandbox/abc"); try { fs::copy_file("sandbox/abc", "sandbox/def"); } catch (fs::filesystem_error& e) { std::cout << "Could not copy sandbox/abc: " << e.what() << '\n'; } fs::remove_all("sandbox"); }
可能的輸出
file1.txt holds: a file2.txt holds: a Could not copy sandbox/abc: copy_file: Is a directory: "sandbox/abc", "sandbox/def"
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3014 | C++17 | error_code 過載被標記為 noexcept 但可以分配記憶體 |
noexcept 已移除 |
[編輯] 另請參閱
(C++17) |
指定複製操作的語義 (列舉) |
(C++17) |
複製一個符號連結 (函式) |
(C++17) |
複製檔案或目錄 (函式) |