在C++中,可以使用File类来进行文件路径操作。以下是一些常见的文件路径操作示例:
#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;
}
#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;
}
#include <iostream>
#include <filesystem>
int main() {
std::filesystem::path dir_path = "path/to/new/dir";
std::filesystem::create_directory(dir_path);
return 0;
}
#include <iostream>
#include <filesystem>
int main() {
std::filesystem::path file_path = "path/to/file.txt";
std::filesystem::remove(file_path);
return 0;
}
这些示例演示了如何使用C++的File类进行文件路径操作,包括获取当前工作目录、检查文件是否存在、创建目录以及删除文件等操作。可以根据需要进行相应的调整和扩展。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:如何结合C++ File类进行文件压缩