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

Beginner
5 min read
Updated: Nov 2026
3 examples
fstream

Definition

In C++, close() finishes work with a file. It writes out any text still waiting in the buffer and then disconnects the stream from the file.

The stream object stays alive and can open another file later with open(). If you forget to call it, the destructor does it for you when the stream goes out of scope. 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 close on a file stream. It takes no arguments.

C++
out << "data";
out.close();           // saved and disconnected
CallWhat it does
out.close()Saves the data and releases the file.
if (out.is_open()) out.close();Closes only when the stream is open.
out.open("other.txt")After close(), the same stream can be used with a new file.

Closing early is useful when you want to read a file that you just wrote with another stream.

Library declarations
C++
void close();

Parameters

ParameterDescription
Noneclose() takes no parameters.

Return Value

Return ValueDescription
Nothingclose() returns void. If the stream was not open, or closing fails, failbit is set.

C++ Examples

Example 1 — is_open() Before and After close()

The stream knows whether it is connected. This program creates close_demo.txt.

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

int main() {
    std::ofstream out("close_demo.txt");

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

How It Works

  1. The constructor opens close_demo.txt, so the first is_open() says yes.
  2. The text is written into the buffer. close() flushes it to the file and disconnects the stream.
  3. Now is_open() says no. The stream object still exists but has no file.

Example 2 — close() Saves the Data

Close the writer first, then read the file with a second stream.

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

int main() {
    std::ofstream out("close_saved.txt");
    out << "line one\n";
    out << "line two\n";
    out.close();

    std::ifstream in("close_saved.txt");
    std::string line;
    int count = 0;
    while (std::getline(in, line)) {
        count++;
    }

    std::cout << "lines in file: " << count << '\n';
    return 0;
}

How It Works

  1. Both lines are written through the same stream and wait in its buffer.
  2. out.close() pushes the buffer to the disk, so the file is complete before it is read.
  3. The loop counts the two lines that the second stream finds.

Example 3 — close() on a Stream That Is Not Open

Closing nothing is an error, and the stream tells you.

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

int main() {
    std::ifstream in;

    in.close();

    std::cout << "is_open: " << (in.is_open() ? "yes" : "no") << '\n';
    std::cout << "fail: " << (in.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. std::ifstream in; creates a stream with no file attached.
  2. in.close() has nothing to close, so it sets failbit.
  3. is_open() was false all the time, and fail() now reports the mistake.

Frequently Asked Questions

It flushes the buffered data to the file and disconnects the file stream from the file.
No. The destructor of ofstream and ifstream closes the file when the stream goes out of scope. Call close() when you need the file free or saved earlier.
Yes. Call open() with a new file name. If failbit is set from an earlier problem, call clear() first.
The second close() has nothing to close and sets failbit. It is harmless but shows a mistake in the logic.
No. It only releases the connection. The file stays on the disk with the data that was written.

Next: eof()

Check whether a read ran past the end of the input.

eof() 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