Questions tagged [iostream]

The C++ iostream library is an object-oriented library that provides input and output functionality using streams. The iostreams classes support type-safe I/O of built-in types and can be extended to support user-defined types by overloading the >> and << operators.

Use this tag for questions about using iostreams, including writing overloaded operators for your own types.

The C++ standard library defines the std::istream, std::ostream and std::iostream base classes, as well as the standard stream objects std::cin, std::cout and std::cerr and derived iostream types for reading/writing files and strings. An iostream object is responsible for formatting operations (such as converting an integer 1234 into the string "1234" or vice versa) but uses a stream buffer (an object derived from std::streambuf) to interface with the underlying data stream. The stream buffer does any buffering of characters, manages the stream position and transports characters to/from an external device such as a file.

Iostreams use locales to support internationalization and are implemented as a hierarchy of class templates to support different types of characters, so that std::istream is actually a typedef for the specialization std::basic_istream<char, std::char_traits<char>>. The first template parameter is the character type used by the stream and the second is a traits class that provides operations for working with the character type.

A quick introduction to iostreams can be found in Chapter 3: A tour of the Standard Library in Stroustrup's TC++PL. Josuttis's The C++ Standard Library gives more information. The most detailed reference on using and extending iostreams is Langer & Kreft's Standard C++ IOStreams and Locales (see excerpt about stream buffers.)

2485 questions
2159
votes
10 answers

Why is reading lines from stdin much slower in C++ than Python?

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I'm not yet an expert Pythonista, please…
JJC
  • 9,547
  • 8
  • 48
  • 53
703
votes
11 answers

"std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this instead: std::cout << "Test line\n"; Is there a technical reason…
Head Geek
  • 38,128
  • 22
  • 77
  • 87
683
votes
5 answers

Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

I just found a comment in this answer saying that using iostream::eof in a loop condition is "almost certainly wrong". I generally use something like while(cin>>n) - which I guess implicitly checks for EOF. Why is checking for eof explicitly using…
MAK
  • 26,140
  • 11
  • 55
  • 86
512
votes
16 answers

'printf' vs. 'cout' in C++

What is the difference between printf() and cout in C++?
hero
  • 5,161
  • 3
  • 16
  • 6
442
votes
16 answers

How do I print a double value with full precision using cout?

In my earlier question I was printing a double using cout that got rounded when I wasn't expecting it. How can I make cout print a double using full precision?
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
305
votes
13 answers

How to print (using cout) a number in binary form?

I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two's complement…
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
280
votes
6 answers

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian…
212
votes
20 answers

Can you explain the concept of streams?

I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn't the backing store itself what we interact…
Rob Sobers
  • 20,737
  • 24
  • 82
  • 111
211
votes
4 answers

Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?

Every time I mention slow performance of C++ standard library iostreams, I get met with a wave of disbelief. Yet I have profiler results showing large amounts of time spent in iostream library code (full compiler optimizations), and switching from…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
145
votes
5 answers

Why does std::getline() skip input after a formatted extraction?

I have the following piece of code that prompts the user for their cat's age and name: #include #include int main() { int age; std::string name; std::cin >> age; std::getline(std::cin, name); if…
David G
  • 94,763
  • 41
  • 167
  • 253
142
votes
9 answers

Restore the state of std::cout after manipulating it

Suppose I have a code like this: void printHex(std::ostream& x){ x<
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
138
votes
11 answers

Who architected / designed C++'s IOStreams, and would it still be considered well-designed by today's standards?

First off, it may seem that I'm asking for subjective opinions, but that's not what I'm after. I'd love to hear some well-grounded arguments on this topic. In the hope of getting some insight into how a modern streams / serialization framework…
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
130
votes
7 answers

Reading from text file until EOF repeats last line

The following C++ code uses a ifstream object to read integers from a text file (which has one number per line) until it hits EOF. Why does it read the integer on the last line twice? How to fix this? Code: #include #include…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
130
votes
7 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I tried researching the difference between cout, cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on…
Arlene Batada
  • 1,565
  • 2
  • 11
  • 11
120
votes
4 answers

Why would we call cin.clear() and cin.ignore() after reading input?

Google Code University's C++ tutorial used to have this code: // Description: Illustrate the use of cin to get input // and how to recover from errors. #include using namespace std; int main() { int input_var = 0; // Enter the do…
JacKeown
  • 2,780
  • 7
  • 26
  • 34
1
2 3
99 100