Input/output library
Curated hub for C++ I/O: stream foundations, file and string streams, print-oriented output, syncstream, and C-style compatibility.
C++ I/O spans several models: classic stream extraction/insertion, file streams, string-backed streams, newer span-backed streams, synchronized output, and modern print-oriented functions. This hub helps you choose the right I/O route first, then drill into the concrete stream or helper type you actually need.
# Start Here
Console and standard streams
Start here when you are writing to or reading from the standard process streams and want the usual stream insertion/extraction workflow.
File streams
Use stream-based file I/O when you want typed extraction/insertion, stream state management, and buffered file access.
String-backed streams
Choose string streams when you want stream formatting/parsing over an in-memory string buffer instead of a file or console stream.
Span-backed streams
Use span streams for stream-style operations over an existing span buffer without owning the underlying storage.
Modern formatted output
Prefer print-oriented APIs when you want direct formatted output without building a stream insertion pipeline manually.
print · println · osyncstream
C compatibility I/O
Use the C-style surface when you need `FILE*`, `printf`/`scanf`-style APIs, or compatibility with existing C code.
# I/O Models
| Model | Start with | Use it for |
|---|---|---|
| Stream foundations | ios_base, basic_ios, basic_streambuf | Understanding stream state, formatting flags, locale interaction, ties, and buffer mechanics. |
| Standard process streams | cout, cin, cerr, clog | Interactive console input/output and process-level logging/error channels. |
| File streams | ifstream, ofstream, fstream | Buffered file input, output, and read/write stream access with stream semantics. |
| String streams | istringstream, ostringstream, stringstream | Parsing or formatting text in memory using the stream interface. |
| Span-backed streams | spanstream, ispanstream, ospanstream | Stream-style access over existing contiguous storage, especially when you want non-owning buffers. |
| Print-oriented output | print, println | Direct formatted output using the modern format-based model instead of stream insertion chains. |
| Synchronized output | basic_osyncstream | Coordinating output from multiple threads without interleaving chunks unpredictably. |
| C compatibility | C I/O | `FILE*`-based APIs, compatibility with C code, and traditional formatted I/O families. |
# Quick Choice Map
| If you need... | Start with | Why | Common alternative |
|---|---|---|---|
| Formatted console output in existing stream-heavy code | cout and ostream | The standard insertion model integrates directly with manipulators and stream state. | |
| Direct formatted output with less stream boilerplate | print or println | Uses the modern formatting route and is often simpler when you only need output. | cout, format |
| File-based text or binary streaming | ifstream, ofstream, or fstream | They provide typed stream operations and file ownership on top of file buffers. | C I/O, filesystem |
| Parsing from or formatting to a `std::string` | istringstream, ostringstream, or stringstream | Lets you reuse the stream extraction/insertion model entirely in memory. | format, string utilities |
| Stream-style operations over a borrowed buffer | spanstream and related span stream types | Useful when a stream interface is convenient but you do not want string ownership or file handles. | stringstream, span |
| Thread-safe grouped output | basic_osyncstream | Buffers output chunks and emits them atomically to the wrapped stream. | cout, print |
| Legacy or C interoperability with `FILE*` | C I/O | Required when working with traditional C APIs or `printf`/`scanf` style code. | filebuf, fstream |
# Modern Vs. Legacy Routes
| Use case | Preferred modern route | Legacy or older route |
|---|---|---|
| Formatted terminal or file output | print, println, ostream | C I/O and manual `printf`-style formatting |
| In-memory stream processing | stringstream, spanstream | strstream, istrstream, ostrstream, strstreambuf |
| Concurrent output to a shared stream | basic_osyncstream | Manual locking around plain ostream operations |
| Path-oriented file work | filesystem plus file streams when needed | Mixing all file/path concerns directly into stream-only code |
# Support Types And Stream Plumbing
| Area | Key pages | Why you visit it |
|---|---|---|
| Formatting flags and stream state | ios_base, basic_ios | When extraction/insertion behavior depends on flags, locale, fill characters, or error bits. |
| Underlying buffering | basic_streambuf, basic_filebuf, basic_stringbuf, basic_spanbuf | When you need lower-level control over the storage or transport behind a stream interface. |
| Manipulators and offsets | manipulators, fpos, streamoff, streamsize | When formatting, positioning, or size/offset types matter directly. |
| I/O error categories | io_errc | When integrating stream errors with the broader `error_code` model. |
# Related Navigation
Filesystem
Use filesystem for path manipulation, directory traversal, file status, and file operations outside stream mechanics.
Format
Use the formatting library when your main question is format-string semantics or formatter customization rather than I/O transport.
String
Use the string hub for owning strings, views, character encodings, literals, and conversions rather than stream wrappers.
Diagnostics
Use diagnostics for exception types, `error_code`, and broader runtime error handling adjacent to I/O failures.