std::experimental::filesystem::remove, std::experimental::filesystem::remove_all
來自 cppreference.com
< cpp | experimental | fs
定義於標頭檔案 <experimental/filesystem> |
||
bool remove( const path& p ); bool remove( const path& p, error_code& ec ); |
(1) | (檔案系統 TS) |
std::uintmax_t remove_all( const path& p ); std::uintmax_t remove_all( const path& p, error_code& ec ); |
(2) | (檔案系統 TS) |
目錄 |
[編輯] 引數
p | - | 要刪除的路徑 |
ec | - | 非丟擲過載中用於錯誤報告的出參 |
[編輯] 返回值
1) 如果檔案被刪除則返回 true,如果檔案不存在則返回 false。接受 error_code& 引數的過載在錯誤時返回 false。
[編輯] 異常
不接受 error_code& 引數的過載在底層作業系統 API 錯誤時丟擲 filesystem_error,構造時將 p 作為第一個引數,作業系統錯誤碼作為錯誤碼引數。如果記憶體分配失敗,可能會丟擲 std::bad_alloc。接受 error_code& 引數的過載在作業系統 API 呼叫失敗時將其設定為作業系統 API 錯誤碼,並且在沒有錯誤發生時執行 ec.clear()。此過載具有noexcept 規範:
noexcept
[編輯] 注意
在 POSIX 系統上,此函式通常根據需要呼叫 `unlink` 和 `rmdir`,在 Windows 上呼叫 `RemoveDirectoryW` 和 `DeleteFileW`。
[編輯] 示例
執行此程式碼
#include <cstdint> #include <experimental/filesystem> #include <iostream> namespace fs = std::experimental::filesystem; int main() { fs::path dir = fs::temp_directory_path(); fs::create_directories(dir / "abcdef/example"); std::uintmax_t n = fs::remove_all(dir / "abcdef"); std::cout << "Deleted " << n << " files or directories\n"; }
可能的輸出
Deleted 2 files or directories
[編輯] 另請參閱
擦除檔案 (函式) |