cppadvanced90 minutes

Advanced File System Explorer and Analyzer

Build a C++ program that recursively explores a given directory, constructs an in-memory model of the entire file system hierarchy starting at that directory, and generates detailed reports including total file counts, directory sizes, and file type distributions. The project tests your skills in recursion, data structures, file I/O, and performance optimization.

Challenge prompt

Create a C++ application that accepts a directory path as input and recursively scans all files and subdirectories in that path. Construct a tree-like data structure representing the directory hierarchy, where each node contains metadata such as name, size, and file type for files or aggregated size for directories. Your program should output: 1) total number of files scanned, 2) aggregated sizes of each directory, 3) distribution count of file types by extension, and 4) the largest file found with its full path. Ensure efficient traversal and minimal memory usage during the process.

Guidance

  • Use recursive functions to traverse directories and build a tree structure representing the file system.
  • Store metadata efficiently within tree nodes to allow aggregate computations like directory sizes and file type counts.
  • Optimize file system access and memory usage, considering large directories and deeply nested structures.
  • Output a clean, readable summary report after traversal completes.

Hints

  • Leverage the C++17 <filesystem> library for directory iteration and metadata access.
  • Use maps or hash tables to track file type counts dynamically during traversal.
  • Consider using smart pointers or appropriate memory management techniques when constructing your in-memory tree.

Starter code

#include <iostream>
#include <filesystem>
#include <map>
#include <memory>

namespace fs = std::filesystem;

struct FileNode {
    std::string name;
    uintmax_t size;
    bool isDirectory;
    std::map<std::string, std::unique_ptr<FileNode>> children;

    FileNode(std::string n, uintmax_t s, bool dir) : name(std::move(n)), size(s), isDirectory(dir) {}
};

std::unique_ptr<FileNode> buildFileTree(const fs::path& path);

int main() {
    std::string rootPath;
    std::cout << "Enter directory path to scan: ";
    std::getline(std::cin, rootPath);
    
    auto root = buildFileTree(rootPath);
    // TODO: implement traversal analysis and reporting

    return 0;
}

// Implement buildFileTree to recursively scan directories and construct tree
std::unique_ptr<FileNode> buildFileTree(const fs::path& path) {
    // Your code here
    return nullptr;
}

Expected output

Total files scanned: 235 Total directories scanned: 45 Directory sizes: /root: 1,234,567 bytes /root/subdir1: 456,789 bytes ... File type distribution: .cpp: 120 .txt: 50 .jpg: 65 Largest file: /root/videos/movie.mp4 (500,000,000 bytes)

Core concepts

recursionfilesystemdata structuresmemory managementperformance optimization

Challenge a Friend

Send this duel to someone else and see if they can solve it.