C ++ and C input / output file

The input / output of C ++ files is more stringent than the input / output of C files. So, in C ++, is creating a new library for input / output of files useful or not? I mean <fstream> Can anyone tell if there are any advantages to I / O C ++ input files?

+6
c ++ file io
source share
8 answers

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

+12
source share

The capture buffer overflow seems like a big win for C ++ for me.

+7
source share

Please take a look

http://www.ddj.com/cpp/184403651

then you would prefer C ++ I / O over CI / O.

shorter than C is preferable if you know the size of the data before reading or writing and for speed. C ++ is preferable if you do not know the size of the data and efficient code.

+6
source share

In response to David Allan Finch's answer, I fixed the error in my test code (he dumped the stream in the C ++ version after each separate line) and repeated the test:

The C ++ loop now looks like this:

 start = time(0); { ofstream outdata("example2.txt"); for( int i = 0; i < max; i++ ) { outdata << teststr << ":" << i << "\n"; // note, \n instead of endl } } end = time(0); 

I run 10,000,000 iterations (10 times more than in the source code, because otherwise the numbers are too small for the time () lousy resolution to give us anything meaningful)) And the output:

 G++ 4.1.2: C FILE: 4s C++ Streams: 6s MSVC9.0: C FILE: 10s C++ Streams: 23s 

(note, the version of MSVC was running on my laptop with a much slower hard drive)

But this gives us a performance difference of 1.5-2.3x, depending on the implementation. and other external factors.

+6
source share

The performance differences between formatting printf () / fwrite format I / O formatting and i + C ++ stream formatting are very implementation dependent. Some implementations (for example, Visual C ++) create their I / O streams on top of FILE * objects, and this, as a rule, increases the complexity of their implementation at runtime. Please note, however, that there were no particular restrictions for implementing the library in this way.

In my opinion, the benefits of C ++ I / O are as follows:

  • Enter security, as mentioned earlier.
  • Flexibility of implementation. Code can be written to perform certain formatting or input to or from a common ostream or istream object. The application can then call this code with any derived streaming object. If the code that I wrote and tested against the file now needs to be applied to a socket, serial port, or some other internal stream, you can create a stream implementation specific to this type of I / O. Extending C style I / O in this way doesn't even come close to the possible.
  • Flexibility in locale settings: C approach to using a single global language, in my opinion, is seriously spoiled. I had cases when I called the library code (DLL), which changed the global locale settings under my code and completely ruined my output. A C ++ stream allows you to embed () any locale in a stream object.
+2
source share

std :: ifstream and std :: ofstream are already in the stl library. You do not need to create your own.

The main advantage of all outputs and inputs is type safety.

+1
source share

C and C ++ are two different languages. The C ++ io file takes some time to get used to, but as soon as you use algorithms, exceptions, etc., they usually fall into place very naturally.

+1
source share

Lot. Disadvantages too. See Frequently Asked Languages ​​for details. In short: security types and user types.

0
source share

All Articles