溫馨提示×

c++中filesystem的用法是什么

c++
小億
208
2024-02-18 16:00:26
欄目: 編程語言

C++17引入了標準庫,該庫提供了一組類和函數,用于處理文件系統中的文件和目錄。使用庫,可以方便地執行文件和目錄的各種操作,如創建、復制、移動、刪除文件或目錄,遍歷目錄,獲取文件屬性等。

以下是庫的一些常見用法:

  1. 創建目錄:
#include <filesystem>

std::filesystem::create_directory("path/to/directory");
  1. 創建文件:
#include <filesystem>

std::ofstream file("path/to/file.txt");
  1. 復制文件:
#include <filesystem>

std::filesystem::copy("source_file.txt", "destination_file.txt");
  1. 移動文件:
#include <filesystem>

std::filesystem::rename("source_file.txt", "destination_file.txt");
  1. 刪除文件或目錄:
#include <filesystem>

std::filesystem::remove("path/to/file_or_directory");
  1. 遍歷目錄:
#include <filesystem>

for (const auto& entry : std::filesystem::directory_iterator("path/to/directory")) {
    std::cout << entry.path() << std::endl;
}
  1. 獲取文件屬性:
#include <filesystem>

std::filesystem::file_status status = std::filesystem::status("path/to/file.txt");
if (std::filesystem::is_regular_file(status)) {
    std::cout << "Regular file" << std::endl;
}

這些是庫中的一些常見用法,可以根據具體需求進行靈活運用。需要注意的是,庫中的類和函數定義在std::filesystem命名空間中。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女