C++ IOStreams are ridiculously inefficient (in most implementations I know of). Often, that is not a concern, but when it is, the library is basically useless. It's also a good point that the syntax is unintuitive (and very, very verbose). The library is complex and needlessly hard to extend. It's not very flexible either. When contrasted against something like the STL, IOStreams really looks like a bad dream. But it's here, and we're stuck with it.
The reason it's here, and the reason it looks like it does, is that it was developed earlier, before C++ was a mature language. Before we had decades of experience telling us what is, and is not, good library design. Before anyone really knew what the options were.
C++ needed an I/O library that was better than C's. And in some important ways, C++ IOStreams are better. They're type-safe and extensible as others have mentioned. By implementing a single operator, I can print out a user-defined class. That couldn't be done with printf
. I also don't have to worry about getting format specifiers wrong and printing out garbage because of a lack of type safety.
Those things needed to be fixed. And hey, in the early days, virtual functions and operator overloading were the shit. It looked cool. Of course libraries wanted to use those features.
The IOStreams library is a compromise between:
- something safer and more extensible than C's
stdio.h
- something efficient
- something well-designed and intuitive
- a library that actually existed at the time when C++ was being standardized. (they had to add something, so they had to choose between the candidates that actually existed at the time.)
The library doesn't achieve all of these, and I believe that today, with our decades of experience with the language, we could have designed a far better library. But back in the mid-90's, when they were looking for an I/O library to add, this was the best they could find.