C++ Iostream Functions and Manipulators

About This Index
This page is the C++ Iostream Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. It covers stream member functions from <iostream>, <fstream>, and <sstream>, and manipulators from <ios> and <iomanip>.
Learn what streams are on /cpp/iostream. Use this page when you need one specific tool such as getline(), fail(), endl, or setw().
A member function is called with a dot, like std::cin.fail(). A manipulator goes inside a stream chain, like std::cout << std::hex. All 60 guides are listed below.
How to Use Iostream Functions
Include the headers you need. Call members with a dot and put manipulators into the stream:
#include <iomanip>
#include <iostream>
#include <string>
int n;
std::string line;
std::cin >> n; // formatted input
if (std::cin.fail()) { // member function
std::cin.clear(); // reset the error flags
}
std::cin.ignore(1000, '\n'); // throw away the rest of the line
std::getline(std::cin, line); // read a whole line
std::cout << std::setw(6) << n // manipulator with argument
<< std::hex << 255 // manipulator: prints ff
<< std::endl; // new line and flush C++ Iostream Functions Table
All guides in this C++ tutorial series (60 tutorials), grouped by purpose.
Stream state
6 guidesAsk whether the last read or write worked, and reset the stream after a failure.
| Name | Description | Tutorial |
|---|---|---|
good() | Check that no error or end-of-input flag is set. | Try it |
bad() | Check for a serious, unrecoverable stream error. | Try it |
fail() | Check whether the last read or write failed. | Try it |
eof() | Check whether a read ran past the end of the input. | Try it |
clear() | Reset the stream error flags so it can be used again. | Try it |
rdstate() | Read all state flags of a stream at once. | Try it |
Characters, lines, and blocks
11 guidesRead and write single characters, whole lines, and raw blocks of text.
| Name | Description | Tutorial |
|---|---|---|
get() | Read one character, spaces and new lines included. | Try it |
getline() | Read a whole line of text, spaces included. | Try it |
put() | Write one character to an output stream. | Try it |
write() | Write a block of characters, with no formatting. | Try it |
read() | Read a fixed number of characters into a buffer. | Try it |
readsome() | Read only the characters that are ready right now. | Try it |
peek() | Look at the next character without removing it. | Try it |
putback() | Push a character back so it is read again. | Try it |
unget() | Step back one character in the input stream. | Try it |
ignore() | Skip and throw away characters from the input. | Try it |
gcount() | Count how many characters the last read took. | Try it |
File and stream control
13 guidesOpen and close files, flush output, move the read or write position, and set field width and flags.
| Name | Description | Tutorial |
|---|---|---|
open() | Connect a file stream to a file on disk. | Try it |
close() | Flush and disconnect a file stream from its file. | Try it |
is_open() | Check whether a file stream is connected to a file. | Try it |
flush() | Send buffered output to its destination now. | Try it |
sync() | Synchronize the input buffer with its source. | Try it |
tie() | Link an input stream to an output stream. | Try it |
seekg() | Move the read position in an input stream. | Try it |
seekp() | Move the write position in an output stream. | Try it |
tellg() | Get the current read position of an input stream. | Try it |
tellp() | Get the current write position of an output stream. | Try it |
width() | Get or set the minimum width of the next output field. | Try it |
setf() | Turn on one or more stream format flags. | Try it |
unsetf() | Turn off one or more stream format flags. | Try it |
Format manipulators
21 guidesPut these into a stream with << or >> to change how numbers, bools, and spaces are handled. They come from <ios>, which iostream includes.
| Name | Description | Tutorial |
|---|---|---|
boolalpha | Print and read bool values as true and false. | Try it |
noboolalpha | Go back to printing bool values as 1 and 0. | Try it |
showbase | Print a prefix such as 0x for hex and 0 for octal. | Try it |
noshowbase | Stop printing the 0x and 0 base prefixes. | Try it |
showpoint | Always print the decimal point and trailing zeros. | Try it |
noshowpoint | Hide a needless decimal point and trailing zeros. | Try it |
showpos | Print a plus sign in front of positive numbers. | Try it |
noshowpos | Stop printing the plus sign for positive numbers. | Try it |
skipws | Skip leading spaces and new lines when reading. | Try it |
noskipws | Read spaces and new lines like normal characters. | Try it |
unitbuf | Flush the stream after every output operation. | Try it |
nounitbuf | Stop flushing after every output operation. | Try it |
uppercase | Use capital letters in hex digits and exponents. | Try it |
nouppercase | Go back to small letters in hex digits and exponents. | Try it |
dec | Read and print integers in base 10. | Try it |
hex | Read and print integers in base 16. | Try it |
oct | Read and print integers in base 8. | Try it |
fixed | Print floating-point numbers with a fixed number of decimals. | Try it |
scientific | Print floating-point numbers in scientific notation. | Try it |
left | Align padded output to the left of its field. | Try it |
right | Align padded output to the right of its field. | Try it |
iomanip and output helpers
8 guidesManipulators that take a value (from <iomanip>) and the common line and flush helpers.
| Name | Description | Tutorial |
|---|---|---|
setw() | Set the field width for the next item only. | Try it |
setfill() | Choose the padding character used with setw. | Try it |
setprecision() | Set how many digits a floating-point number shows. | Try it |
setbase() | Set the number base to 8, 10, or 16. | Try it |
endl | End the line and flush the output. | Try it |
ends | Write a null character to the output stream. | Try it |
flush() | Send buffered output to its destination now. | Try it |
ws | Skip leading whitespace on an input stream. | Try it |
C++23 print
2 guidesThe newer formatted output functions from <print>. They need a C++23 compiler setting.
Frequently Asked Questions
Start with getline()
Read a whole line of text, spaces included. It is the first function most beginners need after cin.
12 people found this page helpful
