Skip to content
C++ Better Explained
Go back
C++ Check if File Exists: 3 Reliable Ways (with Examples)
Edit page

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.


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.

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.

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

MethodC++ versionNotes
std::filesystem::existsC++17+Cleanest, recommended
ifstream + good()AnyWorks everywhere, less precise
fopenAnyC-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.



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.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Next Post
C++ Enum to String: Convert Enums to Text (3 Ways)