【发布时间】:2019-10-21 00:30:02
【问题描述】:
我想知道如何使用C++ 17 的<filesystem> 标头访问当前工作目录的父目录。然后我想从父目录重定向到另一个文件夹来搜索文件。
这是一个概念性示例:
#include <iostream>
#include <filesystem>
using namespace std;
int main()
{
namespace fs = std::filesystem;
fs::path working_dir(fs::current_path());
fs::path parent_path(
// ... Get path to parent dir of current working dir ... //
);
fs::current_path(parent_path);
fs::current_path(
// ... Redirect to subfolder called "sub_fold" ... //
);
// ... Search for specified file called "sub_file" to see if it exists ... //
if (sub_file.exists()) // Psudo-code for returning if "sub_file" exists
{
// ... Do stuff with "sub_file" ... //
}
else cout << "File does not exist in " << fs::current_path << endl;
fs::current_path(working_dir);
// ... Continue program ... //
return 0;
}
【问题讨论】: