Video Coming Soon...

Created by Zed A. Shaw Updated 2026-04-18 04:15:27

34: ls

Implement the first command line tool, ls.

View Source file ls.cpp Only

// https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html
#include <fmt/core.h>
#include <filesystem>

namespace fs = std::filesystem;

int main(int argc, char* argv[]) {
  for(int i = 1; i < argc; i++) {
    fs::path target{argv[i]};

    for(auto& dir_entry : fs::directory_iterator{target}) {
      const std::string& path = dir_entry.path().filename().string();

      if(!path.starts_with(".")) {
        fmt::println("{}", path);
      }
    }
  }
}
Previous Lesson Next Lesson

Register for Learn C++ the Hard Way

Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.