Question
I want to check whether a file exists, using standard C++11, C++14, C++17, or C. I may need to do this for thousands of files, so I want a reliable and efficient approach before processing them.
What can I write in this function?
inline bool exist(const std::string& name)
{
/* SOMETHING */
}
Short Answer
By the end of this page, you will understand the standard ways to check whether a file exists in C++ and C, when to use each approach, and the practical limitations of existence checks. You will also see why std::filesystem::exists is the modern C++17 solution, what to do in C++11/C++14, and how C code commonly handles the same task.
Concept
Checking whether a file exists means asking the operating system whether a path currently refers to something on disk.
In modern C++17, the standard library provides this directly through std::filesystem::exists. In earlier standard C++ versions such as C++11 and C++14, there is no standard filesystem library, so developers often use std::ifstream to try opening the file. In C, a common standard approach is to call fopen and check whether it succeeds.
A key idea is that "exists" is only true at the moment you check. A file can be deleted, moved, or have its permissions changed immediately afterward. Because of that, in real programs it is often better to perform the operation you actually need and handle failure, rather than checking first and then acting.
Why this matters:
- File processing tools need to avoid crashes when paths are missing.
- Import scripts often validate input files before reading them.
- Build tools, log readers, and backup utilities frequently work with many files.
- Good file handling reduces bugs caused by missing files or permission problems.
So the real concept is not just "how do I check existence?" but also how to do file handling safely and idiomatically.
Mental Model
Think of a file path like a house address.
exists(path)asks: "Is there currently a building at this address?"- Opening a file asks: "Can I actually enter and use this building right now?"
Those are related, but not identical.
A house may exist but still be inaccessible because:
- the door is locked (permission denied),
- the address points to a different kind of place (directory instead of file),
- the building is removed right after you check.
So an existence check is like a quick lookup, but the real test is whether the operation you need actually succeeds.
Syntax and Examples
C++17: use std::filesystem::exists
#include <filesystem>
#include <string>
inline bool exist(const std::string& name)
{
return std::filesystem::exists(name);
}
This is the standard C++17 solution. It clearly expresses your intent and works with filesystem paths.
You can also use std::filesystem::path directly:
#include <filesystem>
inline bool exist(const std::filesystem::path& path)
{
return std::filesystem::exists(path);
}
That is often better in real code because filesystem APIs are designed around path.
C++11 / C++14: try opening the file
#include
{
;
file.();
}
Step by Step Execution
Consider this C++17 example:
#include <filesystem>
#include <iostream>
int main()
{
std::string name = "report.txt";
if (std::filesystem::exists(name)) {
std::cout << "File exists\n";
} else {
std::cout << "File does not exist\n";
}
}
Step by step:
namestores the path string"report.txt".std::filesystem::exists(name)asks the operating system about that path.- If the path currently exists, the function returns
true. - The
ifstatement enters the first branch and printsFile exists. - Otherwise, it enters the
elsebranch and printsFile does not exist.
Now compare that with C++11 using ifstream:
Real World Use Cases
Input validation before processing
A batch script may receive a list of CSV files and check whether each path is valid before starting import.
Config file lookup
Applications often look for files such as:
config.json.envsettings.ini
If the file does not exist, they may fall back to defaults.
Log and report generation
A program may check whether an output file already exists before overwriting it.
Asset loading
Games and media tools often verify that images, audio files, or templates are present before loading them.
Backup and synchronization tools
These tools compare expected files with actual files on disk and report missing items.
Real Codebase Usage
In real projects, developers often do more than a simple existence check.
Prefer the real operation when possible
Instead of this:
if (std::filesystem::exists(path)) {
std::ifstream in(path);
// use file
}
many codebases prefer:
std::ifstream in(path);
if (!in) {
// handle error
return;
}
// use file
This avoids a check-then-act race condition.
Guard clauses
A common pattern is to return early when a required file is missing:
if (!std::filesystem::exists(path)) {
return false;
}
Validation pipelines
When processing many files, code often validates each path first and stores missing ones in a list:
std::vector<std::string> missing;
for (const auto& path : paths) {
(!std::filesystem::(path)) {
missing.(path);
}
}
Common Mistakes
1. Assuming "exists" means "readable"
Broken assumption:
if (std::filesystem::exists(path)) {
std::ifstream in(path);
// assume success
}
Why this is a problem:
- the file may exist but lack read permission,
- it may be a directory,
- it may disappear before opening.
Better:
std::ifstream in(path);
if (!in) {
// handle failure
}
2. Forgetting to close FILE* in C
Broken code:
bool exist(const char* name)
{
FILE* file = fopen(name, "r");
return file != NULL;
}
Why this is wrong:
- if
fopensucceeds, the file handle leaks.
Correct version:
Comparisons
| Approach | Language/Version | What it checks | Pros | Cons |
|---|---|---|---|---|
std::filesystem::exists(path) | C++17+ | Whether the path exists | Standard, clear, direct | Requires C++17 |
std::ifstream(name).good() | C++11/14 | Whether file can be opened for reading | Portable in older standard C++ | Not a pure existence check |
fopen(name, "r") | C | Whether file can be opened for reading | Standard C, simple | Must close handle, not pure existence |
| Try actual operation directly | C++/C | Whether the needed action succeeds | Safest real-world pattern |
Cheat Sheet
C++17
#include <filesystem>
bool exists = std::filesystem::exists(path);
Use when you want to know whether a path exists.
C++11 / C++14
#include <fstream>
bool exists = std::ifstream(path).good();
Use when standard pre-C++17 C++ is required.
C
FILE* f = fopen(path, "r");
if (f) {
fclose(f);
}
Important rules
exists(path)is available in standard C++17 and later.exists(path)can return true for a directory too.- Use
is_regular_file(path)if you need a normal file. - Opening a file checks accessibility, not just existence.
- Prefer attempting the real operation when practical.
- A file can change between checking and using it.
Good default choices
- C++17+:
std::filesystem::exists
FAQ
Is std::filesystem::exists the standard C++ way?
Yes. It is the standard solution in C++17 and later.
What should I use in C++11 or C++14?
A common standard approach is std::ifstream(path).good() to test whether the file can be opened for reading.
Is checking existence before opening a file always a good idea?
No. In many cases, it is better to open the file directly and handle failure. That avoids check-then-act issues.
Does exists() check only regular files?
No. It checks whether the path exists at all, which includes directories and other filesystem objects.
How do I check that the path is a normal file in C++17?
Use std::filesystem::is_regular_file(path).
Is ifstream(...).good() exactly the same as file existence?
No. It means the file stream is usable after opening, which depends on permissions and file type too.
Does making the function inline make the file check much faster?
Usually no. The expensive part is the filesystem access, not the function call.
What if I need to check thousands of files?
Use a simple loop and keep the logic clear. If you need only readability or processing, often it is best to try the real operation and collect failures.
Mini Project
Description
Build a small file validator that checks a list of file paths and reports which ones are missing. This is a practical example of validating input before starting a batch process, such as importing data files or checking required resources.
Goal
Create a program that loops through several file paths and prints whether each file exists, along with a final count of missing files.
Requirements
- Read or define a list of file paths to validate.
- Check each path using a standard C++ approach.
- Print a message for each path indicating whether it exists.
- Count how many paths are missing.
- Print a final summary at the end.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C printf Format Specifier for bool: How to Print Boolean Values
Learn how to print bool values in C with printf, why no %b/%B specifier exists, and the common patterns to print true/false or 0/1.
Calling C or C++ from Python: Building Python Bindings
Learn the quickest ways to call C or C++ from Python, including ctypes, C extensions, Cython, and binding tools with practical examples.