skip to content

Iostreams and Buffering

iostreams carry state flags, buffer output, and stay in sync with C stdio unless told not to. Interviewers ask why endl is slow and what happens after a failed extraction.

on this pageshow

explore

questions

5

In C++, what is the difference between writing std::endl and '\n' to an output stream, and when does it matter?

level: juniorimportance: must knowfreq 72%

answer

  1. one of them does two things
  2. a newline plus something else
  3. buffers exist for a reason
  4. loops that print every line

basics

~20 s

std::endl writes a newline and then flushes the stream; '\n' only writes the newline and leaves it in the buffer. In a loop that writes many lines, flushing every line forces a write each time and can make output many times slower.

solid answer

~40 s

`std::endl` is a manipulator defined to call `os.put(os.widen('\n'))` and then `os.flush()`. Writing `'\n'` only puts the character into the stream's buffer. Flushing hands the buffered bytes to the operating system, usually one system call, so `std::endl` on every line of a report or a file defeats buffering: in one measurement, 200,000 lines to a `std::ofstream` took about 300 ms with `std::endl` and about 7 ms with `'\n'`. Use `'\n'` by default. Flush deliberately when someone must see the output now, such as a prompt before reading input (which `std::cin`'s tie to `std::cout` already handles) or a progress line before a long step, with `std::flush` or `std::endl`. `std::cerr` is unit-buffered, so it flushes after every output operation anyway.

code

cpp · 21 lines
cpp
#include <chrono>
#include <fstream>
#include <iostream>

int main() {
    using clock = std::chrono::steady_clock;
    const int lines = 200000;
    auto t0 = clock::now();
    {
        std::ofstream out("endl.txt");
        for (int i = 0; i < lines; ++i) out << i << std::endl;   // flush per line
    }
    auto t1 = clock::now();
    {
        std::ofstream out("newline.txt");
        for (int i = 0; i < lines; ++i) out << i << '\n';        // buffered
    }
    auto t2 = clock::now();
    auto ms = [](auto d) { return std::chrono::duration_cast<std::chrono::milliseconds>(d).count(); };
    std::cout << "endl: " << ms(t1 - t0) << " ms, '\\n': " << ms(t2 - t1) << " ms\n";
}

go deeper

for a junior

Recall that std::endl writes a newline and flushes, while '\n' only writes a newline into the buffer.

for a middle

Explain buffering and flushing, the tie between std::cin and std::cout, and why std::cerr flushes on its own.

for a senior

Diagnose a slow tool or log writer by spotting per-line flushes, and place deliberate flushes where a human or another process waits.

for a principal

Set the team default of '\n' with explicit flushes, and decide how output buffering interacts with crash diagnostics.

## What each one does An output stream such as `std::cout` or a `std::ofstream` does not hand every character to the operating system. It collects characters in a **buffer** and writes the buffer out in larger pieces, because each write to a file, pipe or terminal is comparatively expensive. - `'\n'` (or `"\n"` at the end of a string) inserts one newline character into the buffer. Nothing else happens. - `std::endl` is a **manipulator**, a function the stream calls when you write `<< std::endl`. The standard defines it as `os.put(os.widen('\n'))` followed by `os.flush()`. So it writes a newline **and flushes**. - `std::flush` flushes without writing anything. Flushing means asking the stream buffer to pass everything it holds to its destination at once, typically one system call. ## Why it matters for speed A program that writes a line per loop iteration with `std::endl` performs one flush per line. Writing 200,000 short lines to a `std::ofstream` in one measurement took: | Line ending | Time | |---|---| | `std::endl` | about 300 ms | | `'\n'` | about 7 ms | The exact ratio depends on the platform, the destination and the library, but the direction never changes: a flush per line turns a buffered stream into an almost unbuffered one. This is the most common reason a C++ command-line tool "prints slowly". ## When flushing is the right call Flushing exists because sometimes output must appear **now**: 1. **A prompt before reading input.** You often do not need to flush yourself: `std::cin` is **tied** to `std::cout`, so before `std::cin` reads, it flushes `std::cout` for you. 2. **Progress before a long step.** `std::cout << "loading map..." << std::flush;` makes sure the message is visible before the work starts. 3. **Before a deliberate crash or `std::abort`.** Buffered output is lost if the process dies abruptly, which is why diagnostics belong on `std::cerr`. 4. **Interleaving with another process** reading a pipe line by line, where the reader waits for each line. ## The standard streams are set up differently - `std::cerr` has the `unitbuf` flag set, so it **flushes after every output operation**. Adding `std::endl` to `std::cerr` costs nothing extra in behaviour, only habit. - `std::clog` writes to the same destination as `std::cerr` but without `unitbuf`, so it is buffered. - Whether `std::cout`'s output reaches a terminal at each newline can also depend on the C library, because by default the C++ standard streams are synchronised with C's `stdout`, which is typically line-buffered on a terminal and fully buffered when redirected to a file or pipe. That is why a program can look fine interactively and slow down, or delay output, when redirected. ## Habits to recommend - Write `'\n'` by default. - Flush on purpose with `std::flush`, and only where a human or another process is waiting. - Use `std::cerr` for errors that must survive a crash. - Never use `std::endl` in tight loops, including when writing files or `std::ostringstream` buffers, where flushing achieves nothing useful at all. ## What a flush does and does not promise `flush()` calls `rdbuf()->pubsync()`, which asks the stream buffer to hand its pending characters to the next layer down: the C stream or the operating system. It does **not** promise that the data is on disk; the operating system may still hold it in its own cache. A program that must survive a power loss needs an operating-system call for that, which iostreams does not expose. Related tools on the stream itself: - `std::unitbuf` sets the flag that makes a stream flush after every output operation, as `std::cerr` does from the start; `std::nounitbuf` clears it. - A `std::ofstream` flushes when it is destroyed or closed, so a file written with `'\n'` is complete once the stream goes out of scope. - `std::cout` is flushed at normal program exit, so `'\n'` output is not lost when `main` returns; it is lost only if the process ends abruptly, for example through `std::abort` or a crash. ## A note on the name `endl` reads like "end line", which is why beginners use it everywhere. The name hides the flush. Interviewers ask this question precisely because the difference is invisible in a small program and dominant in a large one.

  • Why does a prompt written with '\n' still appear before std::cin waits for input?
    `std::cin` is tied to `std::cout`: `std::cin.tie()` returns `&std::cout`. Before an input operation, the stream's sentry flushes the tied stream, so pending output, including a prompt without a newline, reaches the screen before the read blocks.
  • Does std::endl on std::cerr cost anything extra?
    Hardly. `std::cerr` has `unitbuf` set, so it already flushes after every output operation. `std::endl` adds one more flush of an already empty buffer. The habit matters more on `std::cout` and file streams, which are buffered.

  • std::endl and '\n' are identical, endl is just more portable
  • std::endl is needed to get a Windows line ending
  • With '\n' the output is only written when the program exits
  • Flushing after every line makes output safer, so always do it
  • std::cerr is buffered, so it needs std::endl to show errors
open as a page

When reading a file line by line with std::ifstream in C++, why is while (!file.eof()) wrong, and what loop should you write?

level: middleimportance: must knowfreq 60%

basics

~20 s

eof() only becomes true after a read has already hit the end, so the loop runs once more and processes a failed read. Write while (std::getline(file, line)), which tests each read's result, and parse each line with a std::istringstream.

open as a page

A C++ tool reading integers with std::cin >> n loops forever when the user types a letter; why, and how do you recover the stream?

level: middleimportance: must knowfreq 65%

basics

~20 s

The failed extraction sets failbit and leaves the letter in the input, so every later >> fails immediately without reading. Test the extraction itself, then call std::cin.clear() and std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n') to skip the bad line.

open as a page

How do you write operator<< and operator>> for a C++ Interval type so it can be printed and parsed with streams?

level: middleimportance: should knowfreq 55%

basics

~20 s

Write them as non-member functions taking the stream by reference and returning it: operator<< takes the Interval by const reference, operator>> by non-const reference. The input operator assigns only after a fully successful read and sets failbit on bad input.

open as a page

A C++ tool reads and prints millions of lines slowly; what do std::ios::sync_with_stdio(false) and std::cin.tie(nullptr) change, and what can they break?

level: seniorimportance: should knowfreq 50%

basics

~20 s

sync_with_stdio(false) lets the C++ standard streams buffer independently of C stdio, and cin.tie(nullptr) stops std::cin flushing std::cout before every read. Both speed up heavy I/O, but afterwards mixing printf with cout can reorder output and prompts may not appear.

open as a page