Video Coming Soon...

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

31: basename/dirname

Implement the first command line tool, basename and dirname.

View Source file basename.cpp Only

//https://www.gnu.org/software/coreutils/manual/html_node/basename-invocation.html 

#include <fmt/core.h>
#include <filesystem>

int main(int argc, char* argv[]) {
  std::string filename{argv[1]};

  std::filesystem::path fp{filename};

  fmt::println("{}", fp.stem().string());
}

View Source file dirname.cpp Only

// https://www.gnu.org/software/coreutils/manual/html_node/dirname-invocation.html

#include <fmt/core.h>
#include <filesystem>

int main(int argc, char* argv[]) {
  std::string filename{argv[1]};

  std::filesystem::path fp{filename};

  fmt::println("{}", fp.parent_path().string());
}
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.