Video Coming Soon...
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());
}
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.