views:

1459

answers:

13

In new C++ code, I tend to use the C++ iostream library instead of the C stdio library.

I've noticed some programmers seem to stick to stdio, insisting that it's more portable.

Is this really the case? What do you use?

+5  A: 

If, like me, you learned C before learning C++, the stdio libraries seem more natural to use. There are pros and cons for iostream vs. stdio but I do miss printf() when using iostream.

Adam Pierce
+6  A: 

Back in the bad old days, the C++ Standards committee kept mucking about with the language and iostreams was a moving target. If you used iostreams, you were then given the opportunity to rewrite parts of your code every year or so. Because of this, I always used stdio which hasn't changed significantly since 1989.

If I were doing stuff today, I would use iostreams.

Colin Jensen
+10  A: 

It's just too verbose.

Ponder the iostream construct for doing the following (similarly for scanf):

// nonsense output, just to examplify
fprintf(stderr, "at %p/%s: mean value %.3f of %4d samples\n",
    stats, stats->name, stats->mean, stats->sample_count);

That would requires something like:

std::cerr << "at " << static_cast<void*>(stats) << "/" << stats->name
          << ": mean value " << std::precision(3) << stats->mean
          << " of " << std::width(4) << std::fill(' ') << stats->sample_count
          << " samples " << std::endl;

String formatting is a case where object-orientedness can, and should be, sidestepped in favour of a formatting DSL embedded in strings. Consider Lisp's format, Python's printf-style formatting, or PHP, Bash, Perl, Ruby and their string intrapolation.

iostream for that use case is misguided, at best.

Mikael Jansson
Disagree (see my post) the fprintf() does zero type checking on input parameters. You are relying on the name,mean,sample_count to be specific types. Any changes to those types will require modification to every printf() in the program.
Martin York
I really can't think of any language that 'stole' iostreams, which probably means it really is as bad an idea as everybody seems to think. :)
Brad Wilson
Use -Wformat (with g++) and you will get printf, etc argument type checking.
rq
Shouldn't the (void*)stats cast in the 2nd code example be static_cast<void*>(stats) to be completely C++?
jk
Brad: I'm pretty sure the Streams in Java are inspired by iostreams, at least their pluggable nature.
Skurmedel
+27  A: 

To answer the original question:
Anything that can be done using stdio can be done using the iostream library.

Disadvantages of iostreams: Its verbose
Advantages    of iostreams: Its easy to extend for new non POD types.

The step forward the C++ made over C was type safety.

  • iostreams was designed to be explicitly type safe. Thus assignment to an object explicitly checked the type (at compiler time) of the object being assigned too (generating an compile time error if required). Thus prevent run-time memory over-runs or writing a float value to a char object etc.

  • scanf()/printf() and family on the other hand rely on the programmer getting the format string correct and there was no type checking (I believe gcc has an extension that helps). As a result it was the source of many bugs (as programmers are less perfect in their analysis than compilers [not going to say compilers are perfect just better than humans]).

Just to clarify comments from Colin Jensen.

  • The iostream libraries have been stable since the release of the last standard (I forget the actual year but about 10 years ago).

To clarify comments by Mikael Jansson.

  • The other languages that he mentions that use the format style have explicit safeguards to prevent the dangerous side effects of the C stdio library that can (in C but not the mentioned languages) cause a run-time crash.

N.B. I agree that the iostream library is a bit on the verbose side. But I am willing to put up with the verboseness to ensure runtime safety. But we can mitigate the verbosity by using Boost Format Library.

#include <iostream>
#include <iomanip>
#include <boost/format.hpp>

struct X
{  // this structure reverse engineered from
   // example provided by 'Mikael Jansson' in order to make this a running example

    char*       name;
    double      mean;
    int         sample_count;
};
int main()
{
    X   stats[] = {{"Plop",5.6,2}};

    // nonsense output, just to exemplify

    // stdio version
    fprintf(stderr, "at %p/%s: mean value %.3f of %4d samples\n",
            stats, stats->name, stats->mean, stats->sample_count);

    // iostream
    std::cerr << "at " << (void*)stats << "/" << stats->name
              << ": mean value " << std::fixed << std::setprecision(3) << stats->mean
              << " of " << std::setw(4) << std::setfill(' ') << stats->sample_count
              << " samples\n";

    // iostream with boost::format
    std::cerr << boost::format("at %p/%s: mean value %.3f of %4d samples\n")
                % stats % stats->name % stats->mean % stats->sample_count;
}
Martin York
What are the crashes are you referring to? I wasn't aware of primitive types carrying runtime information (RTTI) in C++, so in what way does that improve things?
Mikael Jansson
The following is easy to crash:'char s[2];scanf("%s",s);'iostream do type checking at compile time. So problems are discovered at compile time NOT runtime.
Martin York
Another benefit of iostreams is that they work with anything derived from std::iostream. Thus a function which accepts a stream works equally well with console, file, or string input.
rlbond
+3  A: 

For binary IO, I tend to use stdio's fread and fwrite. For formatted stuff I'll usually use IO Stream although as Mikael said, non-trival (non-default?) formatting can be a PITA.

Evan
You can also use istream::read() and ostream::write(), which do unformatted I/O. Mostly useful if you're doing a mix of formatted and unformatted I/O.
Tom
+1  A: 

stdio is better for reading binary files (like freading blocks into a vector<unsigned char> and using .resize() etc.). See the read_rest function in file.hh in http://nuwen.net/libnuwen.html for an example.

C++ streams can choke on lots of bytes when reading binary files causing a false eof.

Shadow2531
If you use the formatted extraction operations, you can have problems with null bytes. But there are also read() and write() methods that work just like fread()/fwrite()
KeithB
also, you can use the lower level streambuf interface which works fine too: istreambuf_iterator<char> it(file), e; vector<char> v(it, e);
Johannes Schaub - litb
+8  A: 

The Boost Format Library provides a type-safe, object-oriented alternative for printf-style string formatting and is a complement to iostreams that does not suffer from the usual verbosity issues due to the clever use of operator%. I recommend considering it over using plain C printf if you dislike formatting with iostream's operator<<.

KK
+2  A: 

I use iostreams, mainly because that makes it easier to fiddle with the stream later on (if I need it). For example, you could find out that you want to display the output in some trace window -- this is relatively easy to do with cout and cerr. You can, off course, fiddle with pipes and stuff on unix, but that is not as portable.

I do love printf-like formatting, so I usually format a string first, and then send it to the buffer. With Qt, I often use QString::sprintf (although they recommend using QString::arg instead). I've looked at boost.format as well, but couldn't really get used to the syntax (too many %'s). I should really give it a look, though.

Jan de Vos
+1  A: 

Since iostreams have become a standard you should use them knowing that your code will work for sure with newer versions of compiler. I guess nowadays most of the compilers know very well about iostreams and there shouldn't be any problem using them.

But if you want to stick with *printf functions there can be no problem in my opinion.

Iulian Şerbănoiu
Unfortunately, they are ABI dependent, so you can't use them for interconnecting plugins developed as shared libraries.
Terminus
+3  A: 

In principle I would use iostreams, in practice I do too much formatted decimals, etc that make iostreams too unreadable, so I use stdio. Boost::format is an improvement, but not quite motivating enough for me. In practice, stdio is nearly typesafe since most modern compilers do argument checking anyway.

It's an area where I'm still not totally happy with any of the solutions.

Dan Hewett
+2  A: 

What I miss about the iolibraries is the formatted input.

iostreams does not have a nice way to replicate scanf() and even boost does not have the required extension for input.

A: 

What about stream input?

+2  A: 

While there are a lot of benefits to the C++ iostreams API, one significant problem is has is around i18n. The problem is that the order of parameter substitutions can vary based on the culture. The classic example is something like:

// i18n UNSAFE 
std::cout << "Dear " << name.given << ' ' << name.family << std::endl;

While that works for English, in Chinese the family name is comes first.

When it comes to translating your code for foreign markets, translating snippets is fraught with peril so new l10ns may require changes to the code and not just different strings.

boost::format seems to combine the best of stdio (a single format string that can use the parameters in a different order then they appear) and iostreams (type-safety, extensibility).

R Samuel Klatchko