HOWTO · C++
How to Check if a File Exists in C++
Check a path with std::filesystem::exists in C++, distinguish files from directories, and build paths under a chosen directory.
On this page
Use std::filesystem::exists to check whether a path names an existing filesystem entry. It returns true for both files and directories. The example below creates a small temporary directory and file so the check has a self-contained input; replace file with the path your program needs to inspect.
Use std::filesystem::exists to Check if a File Exists in a Directory
Run this example:
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
int main() {
namespace fs = std::filesystem;
const auto unique = std::chrono::steady_clock::now().time_since_epoch().count();
const fs::path folder = fs::temp_directory_path() /
("cpp-exists-" + std::to_string(unique));
std::error_code ec;
if (!fs::create_directory(folder, ec) || ec) {
std::cout << "Could not prepare example\n";
return 1;
}
struct cleanup_guard {
fs::path path;
~cleanup_guard() {
std::error_code cleanup_error;
fs::remove_all(path, cleanup_error);
}
} cleanup{folder};
const fs::path file = folder / "notes.txt";
{
std::ofstream output(file);
if (!output) {
std::cout << "Could not prepare example\n";
return 1;
}
}
std::cout << (fs::exists(file) ? "Path exists\n" : "Path does not exist\n");
std::cout << (fs::is_regular_file(file) ? "It is a regular file\n"
: "It is not a regular file\n");
}
Path exists
It is a regular file
The call fs::exists(file) checks the path passed to it. It does not search for a matching filename. Here, folder / "notes.txt" joins a chosen directory with a filename using std::filesystem::path, which handles the platform’s path separator. Use the same pattern with your own directory path when the target is outside the process’s current working directory.
A relative path such as "notes.txt" is resolved from the process’s current working directory, which is set by the program’s launch environment. It is not automatically resolved from the directory containing the executable. To inspect the current working directory, call fs::current_path(); to change it for the process, use fs::current_path(path) and handle any reported filesystem error. Constructing a path from a known directory is usually clearer when you only need to check one location.
exists answers whether an entry is present, not what kind of entry it is. Use fs::is_regular_file(path) when only a regular file should match, or fs::is_directory(path) when you expect a directory. These checks can also fail to provide a usable answer when the program lacks filesystem access or the path changes during inspection. For code that must distinguish those cases, use the overloads that accept std::error_code and inspect the error separately from the returned Boolean value.
For example, std::error_code ec; const bool found = fs::exists(path, ec); lets a caller examine both found and ec: a false result with no error means the path was not found, while a set error describes why the check could not be completed. The overload without an error-code argument reports filesystem failures by throwing an exception. Choose the error-code form in code that needs to continue through expected filesystem problems; otherwise, the throwing form keeps a simple check readable. A path can also stop referring to the same entry between the check and a later open, so use the result as a lookup answer rather than a guarantee that a later operation will succeed.
The example uses std::filesystem and creates its own temporary fixture. If directory or file setup fails, it prints the preparation message and returns a nonzero status. The cleanup guard attempts to remove the temporary directory on normal exits, and cleanup errors are ignored. The query uses the throwing overload, so an uncaught filesystem exception escaping main can terminate the program without guaranteeing that the guard destructor runs. On the normal path, the output is the two lines shown above.