Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which C I/O library should be used in C++ code? [closed]

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 is better to use?

like image 744
Ferruccio Avatar asked Sep 23 '08 04:09

Ferruccio


People also ask

Can I use CPP library in C?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

What is the default stream in C++?

@milleniumbug The standard guarantees that the standard streams are thread-safe if sync_with_stdio is set (which is the default).


1 Answers

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

Disadvantages of iostreams: verbose Advantages    of iostreams: 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; } 
like image 51
11 revs, 2 users 98% Avatar answered Oct 10 '22 23:10

11 revs, 2 users 98%