阅读量:83
在C++中,可以使用File类来进行文件路径操作。以下是一些常见的文件路径操作示例:
- 获取当前工作目录:
#include
#include
int main() {
std::filesystem::path current_path = std::filesystem::current_path();
std::cout << "Current path: " << current class="hljs-keyword">return 0;
}
- 检查文件或目录是否存在:
#include
#include
int main() {
std::filesystem::path file_path = "path/to/file.txt";
if (std::filesystem::exists(file_path)) {
std::cout << "File exists." << std class="hljs-keyword">else {
std::cout << "File does not exist." << std class="hljs-keyword">return 0;
}
- 创建目录:
#include
#include
int main() {
std::filesystem::path dir_path = "path/to/new/dir";
std::filesystem::create_directory(dir_path);
return 0;
}
- 删除文件或目录:
#include
#include
int main() {
std::filesystem::path file_path = "path/to/file.txt";
std::filesystem::remove(file_path);
return 0;
}
这些示例演示了如何使用C++的File类进行文件路径操作,包括获取当前工作目录、检查文件是否存在、创建目录以及删除文件等操作。可以根据需要进行相应的调整和扩展。