check if directory exists cpp
To check if a directory exists in C++, you can use the std::filesystem
library, which was introduced in C++17. Here are the steps:
Include the necessary header file:
cpp #include <filesystem>
Use the
std::filesystem::exists()
function to check if the directory exists. This function takes astd::filesystem::path
object as a parameter and returns abool
indicating whether the directory exists or not. Here's an example:cpp std::filesystem::path directoryPath = "path/to/directory"; bool exists = std::filesystem::exists(directoryPath);
You can then use the value of the
exists
variable to perform further actions based on the existence of the directory. For example:cpp if (exists) { // Directory exists // Perform actions for an existing directory } else { // Directory doesn't exist // Perform actions for a non-existing directory }
That's it! By using the std::filesystem::exists()
function, you can easily check if a directory exists in C++.