C++ Core Guidelines: Iostreams

When you interact with the outside world, the iostream library is the way to go in C++. As always you have to keep a few rules in mind. Let me show, which rules.

 horseshoe bend 1908283 1280


The C++ core guidelines gives a good overview to iostreams: "iostreams is a type safe, extensible, formatted and unformatted I/O library for streaming I/O. It supports multiple (and user extensible) buffering strategies and multiple locales. It can be used for conventional I/O, reading and writing to memory (string streams), and user-defines extensions, such as streaming across networks (asio: not yet standardized)."

Suprisingly for me and in contrast to the fact, that iostreams are quite important and are used by the most C++ developers there are only five rules which a deal with them and this rules have not much content. Here are they:

To make a story out of the rules, I have to add additional information. This is not necessary for the first rule:

SL.io.1: Use character-level input only when you have to

First, here is the bad example from the guidelines. Using character-level input for more than one character:


char c;
char buf[128];
int i = 0;
while (cin.get(c) && !isspace(c) && i < 128)
    buf[i++] = c;
if (i == 128) {
    // ... handle too long string ....

To be honest, this is a terrible solution for a simple job. My remark does not hold for the right way to do it:


string s;
cin >> s;


Presumably, the better way is also the faster way.

The next rule states a no-brainer.

SL.io.2: When reading, always consider ill-formed input

The question is now: How can we deal with bad data? Each stream has a state associated.

State of the Stream

Flags represent the state of the stream. The methods for dealing with these flags need the header <iostream>.


Examples causing the different states of a stream


  • Reading beyond the last valid character.


  • False formatted reading.
  • Reading beyond the last valid character.
  • Opening of a file went wrong.


  • Size of the stream buffer cannot be adjusted.
  • Code conversion of the stream buffer went wrong.
  • A part of the stream threw an exception.

stream.fail() returns whether std::ios::failbit or std::ios::badbit is set.

Reading and setting the state of a stream


  • Initialises the flags and sets the stream in the goodbit state


  • Initialises the flags and set the stream into the state sta.


  • Returns the current state.


  • Sets the additional flag fla.

Operations on a stream have only an effect if the stream is in the goodbit state. If the stream is in the badbit state, it can not be reset to the goodbit state.


// streamState.cpp

#include <ios>
#include <iostream>

int main(){

    std::cout << std::boolalpha << std::endl;

    std::cout <<  "In failbit-state: " << std::cin.fail() << std::endl;
    std::cout << std::endl;
    int myInt;
    while (std::cin >> myInt){
        std::cout << "Output: " << myInt << std::endl; 
        std::cout <<  "In failbit-state: " << std::cin.fail() << std::endl;
        std::cout << std::endl;
    std::cout <<  "In failbit-state: " << std::cin.fail() << std::endl;
    std::cout <<  "In failbit-state: " << std::cin.fail() << std::endl;

    std::cout << std::endl;



The input of the string wrongInput causes the stream std::cin to be in std::ios::failbit state. Consequently, wrongInput and std::cin.fail() cannot be displayed. First, you have to set the stream std::cin in the goodbit state.

You can show your output with printf or with iostreams. My tip is obvious.

SL.io.3: Prefer iostreams for I/O

The following two program displays two times the equivalent formatted data. First, by using printf and a format strings; second, by using iostreams and format manipulators.


// printfIostreams.cpp

#include <cstdio>

#include <iomanip>
#include <iostream>
int main(){
    printf("Characters: %c %c \n", 'a', 65);
    printf("Decimals: %d %ld\n", 2011, 650000L);
    printf("Preceding with blanks: %10d \n", 2011);
    printf("Preceding with zeros: %010d \n", 2011);
    printf("Doubles: %4.2f %E \n", 3.1416, 3.1416);
    printf("%s \n", "From C to C++");
    std::cout << std::endl;
    std::cout << "Characters: " << 'a' << " " <<  static_cast<char>(65) << std::endl;  
    std::cout << "Decimals: " << 2011 << " " << 650000L << std::endl;
    std::cout << "Preceding with blanks: " << std::setw(10) << 2011 << std::endl;
    std::cout << "Preceding with zeros: " << std::setfill('0') << std::setw(10) << 20011 << std::endl;
    std::cout << "Doubles: " << std::setprecision(3) << 3.1416 << " " 
                             << std::setprecision(6) << std::scientific <<  3.1416 << std::endl;
    std::cout << "From C to C++" << std::endl;
    std::cout << std::endl;


As promised, the same output:


Okay, but why should you prefer iostreams to printf? There is a subtle but critical difference between printf and iostreams. The format string with printf specifies the type, and the format of the displayed value, the format manipulator with iostreams specifies only the format. To say it the other way around: The compiler deduces the correct type automatically in case of an isotream.

Let me make my point clear. When you had a bad day or are new to C++ and specify the wrong type in a format string, you get undefined behaviour.

// printfIostreamsUndefinedBehaviour.cpp

#include <cstdio>

#include <iostream>

int main(){
    printf("2011: %d\n",2011);            
    printf("3.1416: %d\n",3.1416);           
    printf("\"2011\": %d\n","2011");           
    // printf("%s\n",2011);    // segmentation fault
    std::cout << std::endl;
    std::cout << "2011: " <<  2011 << std::endl;    
    std::cout << "3.146: " << 3.1416 << std::endl;   
    std::cout << "\"2011\": " << "2011" << std::endl;   
    std::cout << std::endl;


This is how undefined behaviour looks like on my local PC.


Of course, the compiler usually writes in case of a wrong format string a warning, but you have no guarantee. Additionally, I know what often happens when the deadline is passed. You ignore the warnings and maybe, will look at it later. Instead of dealing with errors, don't make errors in the first place.

The difference between printf and iostreams reminds to the most important design guideline from Scott Meyers: "Make interfaces easy to use correctly and hard to use incorrectly."

What's next?

I used in the iostream example format specifiers. To make your life as a software developer easier, you should keep a few of the format manipulators in mind. Let me show in my next post, which ones.



Thanks a lot to my Patreon Supporters: Paul Baxter,  Meeting C++, Matt Braun, Avi Lachmish, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, Ramesh Jangama, G Prvulovic, Reiner Eiteljörge, Benjamin Huth, Reinhold Dröge, Timo, Abernitzke, Richard Ohnemus , Frank Grimm, Sakib, and Broeserl.

Thanks in particular to:  TakeUpCode 450 60     crp4


Get your e-book at Leanpub:

The C++ Standard Library


Concurrency With Modern C++


Get Both as one Bundle

cover   ConcurrencyCoverFrame   bundle
With C++11, C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.  

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.


Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.


Get your interactive course


Modern C++ Concurrency in Practice

C++ Standard Library including C++14 & C++17

educative CLibrary

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.

What's Inside?

  • 140 lessons
  • 110 code playgrounds => Runs in the browser
  • 78 code snippets
  • 55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.

What's Inside?

  • 149 lessons
  • 111 code playgrounds => Runs in the browser
  • 164 code snippets
  • 25 illustrations

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 6500

Yesterday 7564

Week 21921

Month 226968

All 4847862

Currently are 170 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments