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

Beginner
5 min read
Updated: Nov 2026
3 examples
fstream

Definition

In C++, open() connects a file stream to a file. You can call it right after creating the stream, or you can skip it and pass the file name to the constructor, which does the same thing.

You can also choose a mode: read, write, append, or binary. After open(), use is_open() or fail() to see whether it worked. 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 open on an ifstream, ofstream, or fstream. The mode argument is optional.

C++
std::ofstream out;
out.open("data.txt");                       // write, replaces old content
out.open("log.txt", std::ios::app);         // add to the end

std::ifstream in;
in.open("data.txt");                        // read
CallWhat it does
out.open("data.txt")Opens the file for writing. An existing file is emptied first.
out.open("log.txt", std::ios::app)Opens for writing and adds new text at the end.
in.open("data.txt")Opens for reading. It fails if the file does not exist.

std::ofstream out("data.txt"); is the same as creating the stream and calling open(). Most programs use the constructor.

Library declarations
C++
void open(const char* filename, ios_base::openmode mode = default);
void open(const std::string& filename, ios_base::openmode mode = default);

Parameters

ParameterDescription
filenameRequired. The name or path of the file, as a C string or a std::string.
modeOptional. Flags joined with |: std::ios::in, out, app (append), ate (go to the end), trunc (empty the file), and binary. The default depends on the stream type.

Return Value

Return ValueDescription
Nothingopen() returns void. On failure it sets failbit, so check is_open() or fail().

C++ Examples

Example 1 — Create a File, Then Read It Back

This program creates open_demo.txt in the current folder.

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

int main() {
    std::ofstream out;
    out.open("open_demo.txt");
    out << "Hello file\n";
    out.close();

    std::ifstream in;
    in.open("open_demo.txt");

    std::string line;
    std::getline(in, line);
    std::cout << line << '\n';
    return 0;
}

How It Works

  1. out.open("open_demo.txt") creates the file for writing. The stream was made without a file name, so open() connects it.
  2. The text is written, and close() saves it and disconnects out.
  3. A separate ifstream opens the same file with open(), and getline() reads the line back.

Example 2 — Add to a File with std::ios::app

The second open() adds a line instead of replacing the content.

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

int main() {
    std::ofstream out;

    out.open("append_demo.txt");
    out << "first\n";
    out.close();

    out.open("append_demo.txt", std::ios::app);
    out << "second\n";
    out.close();

    std::ifstream in("append_demo.txt");
    std::string line;
    while (std::getline(in, line)) {
        std::cout << line << '\n';
    }
    return 0;
}

How It Works

  1. The first open() uses the default write mode, so the file is created fresh and holds first.
  2. The same stream is closed and opened again with std::ios::app. New text now goes to the end instead of replacing the old text.
  3. The loop reads both lines. Without app, only second would be left.

Example 3 — open() on an Open Stream Fails

A stream can hold only one file at a time. The second open() is refused.

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

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

    out.open("open_second.txt");

    std::cout << "second open failed: " << (out.fail() ? "yes" : "no") << '\n';

    out.clear();
    std::cout << "still writing to the first file: " << (out.is_open() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. The constructor already opened open_first.txt, so out is busy.
  2. out.open("open_second.txt") cannot work on an open stream. It sets failbit and does nothing else.
  3. The first file stays open. After clear() the stream is usable again, and is_open() still says yes.

Frequently Asked Questions

It connects a file stream (ifstream, ofstream, or fstream) to a file on disk so that you can read from it or write to it.
None in effect. std::ofstream out("a.txt") creates the stream and opens the file at once. Calling out.open("a.txt") does the same later.
ios::in (read), ios::out (write), ios::app (append), ios::ate (start at the end), ios::trunc (empty the file) and ios::binary (no text conversion). Join them with |.
Call is_open() or test the stream in an if condition, for example if (!out) { ... }. Failure sets failbit.
An ofstream opened with the default mode truncates the file. Add std::ios::app to keep the old content and write at the end.

Next: peek()

Look at the next character without removing it.

peek() 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.

10 people found this page helpful