溫馨提示×

如何利用C++ File類進行文件路徑操作

c++
小樊
156
2024-08-19 19:47:33
欄目: 編程語言

在C++中,可以使用File類來進行文件路徑操作。以下是一些常見的文件路徑操作示例:

  1. 獲取當前工作目錄:
#include <iostream>
#include <filesystem>

int main() {
    std::filesystem::path current_path = std::filesystem::current_path();
    std::cout << "Current path: " << current_path << std::endl;
    return 0;
}
  1. 檢查文件或目錄是否存在:
#include <iostream>
#include <filesystem>

int main() {
    std::filesystem::path file_path = "path/to/file.txt";
    if (std::filesystem::exists(file_path)) {
        std::cout << "File exists." << std::endl;
    } else {
        std::cout << "File does not exist." << std::endl;
    }
    return 0;
}
  1. 創建目錄:
#include <iostream>
#include <filesystem>

int main() {
    std::filesystem::path dir_path = "path/to/new/dir";
    std::filesystem::create_directory(dir_path);
    return 0;
}
  1. 刪除文件或目錄:
#include <iostream>
#include <filesystem>

int main() {
    std::filesystem::path file_path = "path/to/file.txt";
    std::filesystem::remove(file_path);
    return 0;
}

這些示例演示了如何使用C++的File類進行文件路徑操作,包括獲取當前工作目錄、檢查文件是否存在、創建目錄以及刪除文件等操作??梢愿鶕枰M行相應的調整和擴展。

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