C++で指定したパス以下のディレクトリ/ファイル名を取得する
#include <iostream> #include <filesystem> #include <string> #include <vector> #include<algorithm> using namespace std; using std::filesystem::directory_iterator; using std::filesystem::is_directory; void fileSearch(string); vector<string> allPath; int main() { string input = ""; cin >> input; allPath.push_back(input); fileSearch(input); for (int i = 0; i < allPath.size(); i++) cout << allPath[i] << endl; return 0; } void fileSearch(string path) { vector<string> currentPath; for (const auto& file : directory_iterator(path)) { string tmp = file.path(); cout << tmp << endl; currentPath.push_back(tmp); } sort(currentPath.begin(), currentPath.end()); for (int i = 0; i < currentPath.size(); i++) { string tmp = currentPath[i]; allPath.push_back(tmp); if (is_directory(tmp)) fileSearch(tmp); } }
さしあたり
ソートしているから見やすくなっているはず...
(c++のはてな記法の ">|あれ|" あれってなんなのだ...