How to count the number of files in a directory using standard?

前端 未结 3 1528
时光说笑
时光说笑 2021-01-01 21:30

The new standard expected for 2017 adds std::filesystem. Using it, how can I count the number of files (including sub-directories) in a directory?

I kno

3条回答
  •  天命终不由人
    2021-01-01 22:07

    If you are using Visual Studio 17 you need to use the following namespace.

    namespace fs = std::experimental::filesystem;
    

    Then you could probably use a function like this one.

    int Count() {
    int count=0;
    for (auto& p : fs::directory_iterator(dir)) {
        count++;
    
    }
        return count;
    }
    

提交回复
热议问题