#includenamespace fs = boost::filesystem; int main() { fs::path p("/usr/local/include/boost/filesystem.hpp"); std::string file_name = p.filename().string(); std::cout << file_name << std::endl; // Output: filesystem.hpp return 0; }
#includeIn this example, we create a path object `p` that represents the directory `/usr/local/include/boost`. We use the `is_directory()` method to check if the path is a directory, and if so, we output its name along with the information that it is indeed a directory. The Boost.Filesystem library is part of the Boost C++ Libraries package.namespace fs = boost::filesystem; int main() { fs::path p("/usr/local/include/boost"); if (fs::is_directory(p)) { std::cout << p.filename() << " is a directory" << std::endl; // Output: boost is a directory } else if (fs::is_regular_file(p)) { std::cout << p.filename() << " is a file" << std::endl; } else { std::cout << p.filename() << " is neither a file nor a directory" << std::endl; } return 0; }