Post

(C++) directory listing (traversal)

원래는 boost에 있는 API이고, C++ 17에서 표준으로 추가되었기 때문에, boost를 추가하거나 컴파일러를 C++17로 변경해야 한다. 후자가 더 편하기 때문에, 프로젝트 설정 - C/C++ - Language - C++ Language Standard를 /std:c++17로 변경해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
using namespace std;

  

int main(int argc, char \*\*argv)
{
for (auto& p : fs::recursive\_directory\_iterator(root\_path)) {
/\* .path() 메서드가 다시 path 객체를 리턴하기 때문에
C style 입출력을 사용하지 못함. << operator 사용해야 한다.\*/
printf("[%d] ", p.is\_directory());
cout << p.path() << ' ' << p.file\_size() << " Byte\n";

  

// C style의 입출력을 사용하려면 다음과 같이 하면 된다.
printf("%s\n", p.path().string().c\_str());
}
return 0;
}

This post is licensed under CC BY 4.0 by the author.