Opinion
I do not know any real project that uses C ++ streams. They are too slow and difficult to use. There are several newer libraries, such as FastFormat and Boost , which claim to have a snippet in the latest ACCU Overload log. Personally, I have been using the FILE library for the past 15 years or so in C ++, and I see no reason to change.
Speed
Here is a small test program (getting lost quickly) to show the main problem with speed:
#include <stdio.h> #include <time.h> #include<iostream> #include<fstream> using namespace std; int main( int argc, const char* argv[] ) { const int max = 1000000; const char* teststr = "example"; int start = time(0); FILE* file = fopen( "example1", "w" ); for( int i = 0; i < max; i++ ) { fprintf( file, "%s:%d\n", teststr, i ); } fclose( file ); int end = time(0); printf( "C FILE: %ds\n", end-start ); start = time(0); ofstream outdata; outdata.open("example2.dat"); for( int i = 0; i < max; i++ ) { outdata << teststr << ":" << i << endl; } outdata.close(); end = time(0); printf( "C++ Streams: %ds\n", end-start ); return 0; }
And the results on my pc:
C FILE: 5s C++ Streams: 260s Process returned 0 (0x0) execution time : 265.282 s Press any key to continue.
As we can see, this simple example is 52x slower. I hope there are ways to make it faster!
NOTE: changing endl to '\ n' in my example improved C ++ streams, making it only 3 times slower than FILE * streams (thanks to jalf ), maybe there might be ways to do this faster.
Complexity of use
I cannot argue that printf () is not short, but more flexible (IMO) and easier to understand once you go through the initial WTF for macro codes.
double pi = 3.14285714; cout << "pi = " << setprecision(5) << pi << '\n'; printf( "%.5f\n", pi ); cout << "pi = " << fixed << showpos << setprecision(3) << pi << '\n'; printf( "%+.3f\n", pi ); cout << "pi = " << scientific << noshowpos << pi<< '\n'; printf( "%e\n", pi );
Question
Yes, maybe you need a better C ++ library, many of them will be FastFormat - this is a library, time will tell.
Dave