C++:
Checking if a directory exists
How to:
In modern C++ (C++17 and beyond), you can use the filesystem library to check if a directory exists. It provides a straightforward and standardized way to perform filesystem operations, including checking for the existence of a directory.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
const fs::path dirPath = "/path/to/directory";
if (fs::exists(dirPath) && fs::is_directory(dirPath)) {
std::cout << "The directory exists." << std::endl;
} else {
std::cout << "The directory does not exist." << std::endl;
}
return 0;
}
Sample output if the directory exists:
The directory exists.
Sample output if the directory does not exist:
The directory does not exist.
For projects that are not yet using C++17 or for additional features, the Boost Filesystem library is a popular third-party choice that offers similar functionality.
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main() {
const fs::path dirPath = "/path/to/directory";
if (fs::exists(dirPath) && fs::is_directory(dirPath)) {
std::cout << "The directory exists." << std::endl;
} else {
std::cout << "The directory does not exist." << std::endl;
}
return 0;
}
Using Boost Filesystem, the output would be identical to the C++17 filesystem example, depending on the existence of the directory at the specified path.