Alex Rivera | Logout

Should I switch to C++ I/O streams?

Asked 2010-12-02T20:27:40.943
13

I've never been much to use C++ I/O streams and have always opted for what I know. i.e. the printf functions.

I know there are some benefits to using I/O streams, but I'm looking for some tips from the stackoverflow community to help me (or convince me) to switch. Because I still prefer printf and I think the printf style is so much easier to read and quicker to type.

I would still like to be familiar with it even if I still continue to use printf.


Edit. Interestingly, google C++ coding style forbids the use of streams except for logging. See: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Streams

Use streams only for logging. Definition: Streams are a replacement for printf() and scanf().

Pros: With streams, you do not need to know the type of the object you are printing. You do not have problems with format strings not matching the argument list. (Though with gcc, you do not have that problem with printf either.) Streams have automatic constructors and destructors that open and close the relevant files.

Cons: Streams make it difficult to do functionality like pread(). Some formatting (particularly the common format string idiom %.*s) is difficult if not impossible to do efficiently using streams without using printf-like hacks. Streams do not support operator reordering (the %1s directive), which is helpful for internationalization.

Decision: Do not use streams, except where required by a logging interface. Use printf-like routines instead.

There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.

Extended Discussion

Edit
Report

4 Answers

3

You cannot extend printf with new format specifiers to handle your own types.

answered 2010-12-02T20:35:08.417
2

You get more protection against type errors with C++ I/O streams, but they are SLOW. So it mostly depends on how important performance is.

I/O streams are also polymorphic, unlike the C stdio functions, the user of a stream doesn't need to know whether it's connected to a console, file, or some other logic. But this isn't important for most applications.

answered 2010-12-02T20:36:11.117
1

I switched to iostreams after I learned C++ 10 years ago. Several years ago I was convinced that it was a bad choice so I switched back. I did not regret the switch. For a good C++ programmer you need to know both.

Google C++ style guide prefers to use printf/sprint/snprint over iostreams. It says to use iostreams only for logging.

In terms of iostreams benefits:

  • Type safety. Compiler (gcc) can detect type errors, as well as all static analysis tools. Even there is a type error, one can spot the error easily from the print out.
  • Extensibility. Yes iostreams have overloading but all data members eventually go to POD types.
  • No buffer overrun. Use snprintf to overcome the buffer size issue.

Now come the benefits of sprintf:

  • Much better readability. "Record(%d): %s\n" is much easier to read than os << "Record(" << i << ") " << msg << endl;
  • Performance. If you are doing a lot of iostreams stuff, changing them significantly improves performance. I once worked on a library that uses stringstream to convert int/doubles to strings. I replaced with sprintf and performance has improved a lot (there are a lot of calls to the conversion routine). For the record, boost::format has even worse performance.

My conclusion is that to use iostreams sparely. I use it to read memory buffer, or write to memory buffer occasionally. For other work, I use plain C function.

answered 2010-12-03T01:06:17.480
0

To follow up on Noah's answer, boost's format library allows for several different syntaxes, including printf like syntax. It works by overloading the % operator, and so at first appears a little strange. But, something I rather like, it allows you to reuse arguments. For example

cout << format("%1% %2% %1%") % 1 % 2 << endl;

will print

1 2 1

It's pretty versatile and if you can get used to the % signs everywhere, gives you the best of both worlds. As I agree, printf style is often much much easier.

http://beta.boost.org/doc/libs/1_43_0/libs/format/doc/format.html

answered 2010-12-03T01:56:43.950

Your Answer