C++ Check if File Exists: 3 Reliable Ways
The cleanest way to check if a file exists in C++ is std::filesystem::exists("file.txt") (C++17 and later), which returns true when the path exists. If you’re on an older compiler, you can open the file with ifstream and test whether it succeeded. Here are three methods and when to use each.
Method 1: std::filesystem::exists (C++17, Recommended)
Since C++17, the standard library has a dedicated function for exactly this:
#include <iostream>
#include <filesystem>
int main() {
if (std::filesystem::exists("data.txt")) {
std::cout << "The file exists.\n";
} else {
std::cout << "The file does not exist.\n";
}
return 0;
}
This reads like plain English and is the clearest option. It works for files and directories, and it doesn’t open or lock the file. Compile with g++ -std=c++17. This should be your default choice on any modern setup.
Method 2: Open with ifstream (Works Everywhere)
Before C++17, the common trick was to try opening the file and check whether the stream is in a good state:
#include <iostream>
#include <fstream>
bool fileExists(const std::string& name) {
std::ifstream f(name);
return f.good(); // true if the file opened successfully
}
int main() {
std::cout << (fileExists("data.txt") ? "exists\n" : "missing\n");
return 0;
}
good() returns true if the stream opened without errors. This works in any C++ version, which is its main appeal. The downside: it can’t tell you why a file failed to open — a missing file and a permissions problem look the same.
Method 3: C-style fopen
If you’re working in C-flavored code, you can use fopen from <cstdio>:
#include <cstdio>
int main() {
FILE* f = std::fopen("data.txt", "r");
if (f) {
std::printf("exists\n");
std::fclose(f); // always close what you open
} else {
std::printf("missing\n");
}
return 0;
}
This is portable and old, but it has a trap: if fopen succeeds you must call fclose, or you leak a file handle. Prefer Method 1 or 2 in real C++ code.
Which Method Should You Use
| Method | C++ version | Notes |
|---|---|---|
std::filesystem::exists | C++17+ | Cleanest, recommended |
ifstream + good() | Any | Works everywhere, less precise |
fopen | Any | C-style, must remember fclose |
If you can use C++17, reach for std::filesystem::exists every time. The ifstream method is the reliable fallback for older toolchains.
A Word of Caution
Checking existence and then opening the file is technically two steps, and the file could change in between (this is called a race condition). For most beginner programs that’s not a concern, but if it matters, just try to open the file and handle failure directly rather than checking first.
Related Articles
- C++ Read File Line by Line — opening and reading files
- C++ Read CSV File — a practical file-reading task
- C++ Error Messages Explained — decoding common errors
- C++ String Handling — working with file paths as strings
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.