A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://en.cppreference.com/w/cpp/header/../filesystem/recursive_directory_iterator/depth.html below:

std::filesystem::recursive_directory_iterator::depth - cppreference.com

int depth() const;

(since C++17)

Returns the number of directories from the starting directory to the currently iterated directory, i.e. the current depth of the directory hierarchy.

The starting directory has depth of 0, its subdirectories have depth 1, etc.

The behavior is undefined if *this is the end iterator.

[edit] Parameters

(none)

[edit] Return value

Current depth of the directory hierarchy.

[edit] Exceptions

Throws nothing.

[edit] Example

This example uses iteration depth to calculate the indentation of a directory tree printout.

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
 
int main()
{
    fs::current_path(fs::temp_directory_path());
    fs::create_directories("sandbox/a/b/c");
    fs::create_directories("sandbox/a/b/d/e");
    std::ofstream("sandbox/a/b/file1.txt");
    fs::create_symlink("a", "sandbox/syma");
    for (auto i = fs::recursive_directory_iterator("sandbox");
         i != fs::recursive_directory_iterator();
         ++i)
    {
        std::cout << std::string(i.depth() << 1, ' ') << *i;
        if (fs::is_symlink(i->symlink_status()))
            std::cout << " -> " << fs::read_symlink(*i);
        std::cout << '\n';
    }
    fs::remove_all("sandbox");
}

Output:

"sandbox/syma" -> "a"
"sandbox/a"
  "sandbox/a/b"
    "sandbox/a/b/d"
      "sandbox/a/b/d/e"
    "sandbox/a/b/file1.txt"
    "sandbox/a/b/c"

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4