Ch11.12.1: Complete C++ I/O Reference
Stream Classes
| Class | Description |
|---|---|
std::istream | Base input stream |
std::ostream | Base output stream |
std::iostream | Base input/output stream |
std::ifstream | Input file stream |
std::ofstream | Output file stream |
std::fstream | Input/output file stream |
std::istringstream | Input string stream |
std::ostringstream | Output string stream |
std::stringstream | Input/output string stream |
std::filebuf | File buffer (narrow) |
std::wfilebuf | File buffer (wide) |
std::streambuf | Base stream buffer |
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
// File streams
std::ifstream in("input.txt");
std::ofstream out("output.txt");
std::fstream both("both.txt", std::ios::in | std::ios::out);
// String streams
std::istringstream sin("42 3.14");
std::ostringstream sout;
sout << "Value: " << 42;
int x;
double y;
sin >> x >> y;
std::cout << sout.str() << '\n';
return 0;
}
Standard Stream Objects
| Object | Description |
|---|---|
std::cin | Standard input stream |
std::cout | Standard output stream |
std::cerr | Standard error stream (unbuffered) |
std::clog | Standard error stream (buffered) |
std::wcin | Wide standard input stream |
std::wcout | Wide standard output stream |
std::wcerr | Wide standard error stream (unbuffered) |
std::wclog | Wide standard error stream (buffered) |
Stream Manipulators
| Manipulator | Description |
|---|---|
std::endl | Output newline and flush |
std::ends | Output null terminator |
std::flush | Flush stream |
std::ws | Skip whitespace (input) |
std::boolalpha | Use true/false for booleans |
std::noboolalpha | Use 1/0 for booleans |
std::showbase | Show base prefix (0x, 0) |
std::noshowbase | Don’t show base prefix |
std::showpoint | Show decimal point |
std::noshowpoint | Don’t show decimal point |
std::showpos | Show + sign for positive numbers |
std::noshowpos | Don’t show + sign |
std::skipws | Skip whitespace on input |
std::noskipws | Don’t skip whitespace |
std::uppercase | Use uppercase for hex/scientific |
std::nouppercase | Use lowercase |
std::unitbuf | Flush after each output |
std::nounitbuf | Don’t flush after each output |
std::internal | Internal padding |
std::left | Left-align |
std::right | Right-align |
std::dec | Decimal base |
std::hex | Hexadecimal base |
std::oct | Octal base |
std::fixed | Fixed-point notation |
std::scientific | Scientific notation |
std::defaultfloat | Default floating-point notation |
#include <iostream>
#include <iomanip>
int main() {
// Boolean formatting
std::cout << std::boolalpha << true << '\n'; // true
std::cout << std::noboolalpha << true << '\n'; // 1
// Numeric base
std::cout << std::hex << 255 << '\n'; // ff
std::cout << std::showbase << std::hex << 255 << '\n'; // 0xff
// Floating-point
double pi = 3.14159;
std::cout << std::fixed << pi << '\n'; // 3.141590
std::cout << std::scientific << pi << '\n'; // 3.141590e+00
// Alignment
std::cout << std::left << std::setw(10) << 42 << "|\n"; // 42 |
std::cout << std::right << std::setw(10) << 42 << "|\n"; // 42|
return 0;
}