C++ std::ostream::flush() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, output is usually buffered: text collects in memory and is sent to the screen or file later, in one go. flush() forces that to happen now.

It comes in two forms: the member function cout.flush() and the manipulator cout << std::flush. Both do the same job and neither adds a new line. Include <iostream> to use them.

This is a member function of std::ostream. 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

Use the member function, or put the std::flush manipulator into the stream.

C++
std::cout.flush();            // member function
std::cout << "Loading" << std::flush;   // manipulator form
CallWhat it does
std::cout.flush()Sends all waiting output to the screen.
std::cout << std::flushSame job, written inside an output chain.
std::cout << std::endlWrites a new line and then flushes. Use ' ' when you do not need the flush.

endl is a new line plus a flush. flush alone adds no new line.

Library declarations
C++
ostream& flush();

// manipulator
ostream& flush(ostream& os);

Parameters

ParameterDescription
Noneflush() takes no parameters.

Return Value

Return ValueDescription
ostream&A reference to the stream. If the flush failed, badbit is set.

C++ Examples

Example 1 — Show a Prompt Without a New Line

Flush so that the text is visible before the program does more work.

C++
#include <iostream>

int main() {
    std::cout << "Loading" << std::flush;

    for (int i = 0; i < 3; i++) {
        std::cout << '.' << std::flush;
    }

    std::cout << " done\n";
    return 0;
}

How It Works

  1. std::cout << "Loading" puts the text into the buffer. std::flush sends it to the screen right away.
  2. Each loop round writes one dot and flushes it, so a user would see the dots appear one by one.
  3. The last line adds done and a new line. The final text on screen is Loading... done.

Example 2 — The Member Form cout.flush()

The member function does the same thing as the manipulator.

C++
#include <iostream>

int main() {
    std::cout << "A";
    std::cout.flush();

    std::cout << "B";
    std::cout.flush();

    std::cout << '\n';
    return 0;
}

How It Works

  1. std::cout << "A" writes into the buffer, and std::cout.flush() pushes it out at once.
  2. The same happens with B. Because no new line was written between them, they appear side by side.
  3. The final '\n' moves to the next line. It is not flushed by itself, but the program exit flushes everything.

Example 3 — Flush a File Before Reading It

Write to a file and flush, then read the file with a second stream while the first one is still open. The temporary file flush_demo.txt is created in the current folder.

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

int main() {
    std::ofstream out("flush_demo.txt");
    out << "saved\n";
    out.flush();

    std::ifstream in("flush_demo.txt");
    std::string line;
    std::getline(in, line);

    std::cout << "read back: " << line << '\n';
    return 0;
}

How It Works

  1. out << "saved\n" puts the text in the file stream buffer, not yet in the file.
  2. out.flush() writes the buffer to the file, even though out is still open.
  3. The second stream in can now read the line. Without the flush, the file might still be empty at that moment.

Frequently Asked Questions

It sends all buffered output of a stream to its destination, such as the screen or a file, right away.
endl writes a new line and then flushes. flush only flushes. If you need only a new line, write '\n', which is faster.
Yes, at the end of the program, when the buffer is full, when you read from the tied cin, or when you write endl. You flush by hand when you need output to appear earlier.
There is no difference in effect. One is a member call and the other is a manipulator inside a stream chain.
Yes. close() and the destructor of a file stream flush the buffer. flush() is for when you must save the data while the file stays open.

Next: gcount()

Count how many characters the last read took.

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

12 people found this page helpful