C++ std::fstream::is_open() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
fstream

Definition

In C++, is_open() tells you whether a file stream is currently connected to a file. It returns true after a successful open and false before opening, after close(), and when opening failed.

It is the standard check after trying to open a file, for example to read a file that may not exist. Include <fstream> to use it.

This is a member function of std::fstream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call is_open on an ifstream, ofstream, or fstream. It takes no arguments.

C++
std::ifstream in("notes.txt");
if (!in.is_open()) {
    std::cerr << "cannot open notes.txt\n";
    return 1;
}
CallWhat it does
in.is_open()Returns true when a file is open.
!in.is_open()True when opening failed or the stream is closed.
if (!in)A shorter test. It checks the stream flags instead, but it is also true for a failed open.

Check is_open() right after opening. Reading from a stream that never opened just fails quietly.

Library declarations
C++
bool is_open() const;

Parameters

ParameterDescription
Noneis_open() takes no parameters.

Return Value

Return ValueDescription
trueThe stream is attached to an open file.
falseNo file is open: never opened, opening failed, or already closed.

C++ Examples

Example 1 — is_open() Through the Life of a Stream

Check before opening, after opening, and after closing. The program creates is_open_demo.txt.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ofstream out;
    std::cout << "created:  " << (out.is_open() ? "open" : "closed") << '\n';

    out.open("is_open_demo.txt");
    std::cout << "opened:   " << (out.is_open() ? "open" : "closed") << '\n';

    out.close();
    std::cout << "closed:   " << (out.is_open() ? "open" : "closed") << '\n';
    return 0;
}

How It Works

  1. A stream made without a file name has nothing attached, so it starts closed.
  2. After open() works, is_open() returns true and the program prints open.
  3. close() disconnects the file, so the last check prints closed again.

Example 2 — Detect a File That Does Not Exist

The usual guard: stop with a clear message when the file cannot be opened.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ifstream in("definitely_missing_file.txt");

    if (!in.is_open()) {
        std::cout << "Cannot open the file.\n";
        return 1;
    }

    std::cout << "File opened.\n";
    return 0;
}

How It Works

  1. The constructor tries to open a file that does not exist. A read-only stream cannot create it.
  2. The open fails, so is_open() is false and the if branch runs.
  3. The program ends with return 1, a common way to say that something went wrong.

Example 3 — Use is_open() in a Helper Function

The function returns whether the file could be written. One name works, the other points into a folder that does not exist.

C++
#include <fstream>
#include <iostream>
#include <string>

bool save(const std::string& name, const std::string& text) {
    std::ofstream out(name);
    if (!out.is_open()) {
        return false;
    }
    out << text << '\n';
    return true;
}

int main() {
    std::cout << "is_open_save.txt: " << (save("is_open_save.txt", "hi") ? "ok" : "failed") << '\n';
    std::cout << "no_such_folder/x.txt: " << (save("no_such_folder/x.txt", "hi") ? "ok" : "failed") << '\n';
    return 0;
}

How It Works

  1. save() opens an ofstream with the given name and uses is_open() to decide what to return.
  2. The first name is in the current folder, so the file opens and the function returns true.
  3. The second name points into a folder that does not exist. The open fails, is_open() is false, and the function returns false.

Frequently Asked Questions

It returns true if the file stream is attached to an open file, and false if not.
Almost. Both are false when opening failed. But if (file) also becomes false after a failed read, while is_open() stays true as long as the file is open.
The file may not exist, the path may be wrong, or you may have no permission. Check the file name and the folder from which the program runs.
No. It exists only on file streams (ifstream, ofstream, fstream) and the file buffer.
Not required, but closing a stream that is not open sets failbit. A check like if (out.is_open()) avoids that.

Next: open()

Connect a file stream to a file on disk.

open() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful