On this page
  1. std::iostream & std::fstream
  2. Overview
  3. Notes & Pitfalls
  4. Example

std::iostream & std::fstream

Prev: std::filesystem::path | Up: io_and_filesystem | Next: Language & RAII
Rel: std::format & std::print, RAII & Destructors
Ref: cppreference — Input/Output Library
Tags: #public #prog #cpp #stdlib #io #tier/standard


Overview

<iostream> (std::cin, std::cout, std::cerr) and <fstream> (std::ifstream, std::ofstream) provide RAII stream-based I/O—file streams automatically flush and close in their destructors.

Notes & Pitfalls

Example

#include <cassert>
#include <sstream>
#include <string>

int main() {
    std::istringstream in("first\nsecond\n");
    std::string line;
    int count = 0;
    while (std::getline(in, line)) ++count;
    assert(count == 2);
}

Linked from

Related notes