【发布时间】:2020-06-21 21:44:31
【问题描述】:
我目前正在编写一个程序,列出当前目录中的所有文件和子目录。
我正在为更改目录的能力而苦苦挣扎,因此在列出所有目录和子目录后,我需要能够更改目录。在我的代码中,我有fs::is_directory(myPath),它告诉我这个文件是否是一个目录,如果它是我想指向它的路径,然后从里面打印文件。我什至可以使用parent_path 来做到这一点,但我不知道该怎么做。
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main()
{
fs::path myPath("C:\\Users\\xyz\\Desktop\\things");
cout << "your path is : " << myPath << endl;
cout << "name of this directory is : "<< myPath.filename() << endl;
cout << fs::is_directory(myPath) << endl;
cout << myPath.parent_path() << endl;
for(auto file : fs::directory_iterator(myPath)) {
cout << file << " " << fs::is_directory(file) << endl;
}
return 0;
}
【问题讨论】:
标签: c++ c++17 working-directory