C++ file I/O is tougher than C file I/O.
So in C++, creating a new library for file I/O is useful or not? I mean
Can anyone please tell are the
Lots. Drawbacks too. See C++ language FAQ for details. In short: type-safety and user-defined types.
C and C++ are two different languages. C++ file io takes some time getting used to, but once you are using algorithms, exceptions etc they tend to fall into place very naturally.
In response to David Allan Finch's answer, I fixed an error in his benchmarking code (he flushed the stream in the C++ version after every single line), and reran 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 10000000 iterations (10 times more than in the original code, because otherwise, the numbers are just too small for time()'s lousy resolution to give us anything meaningful)) And the output is:
G++ 4.1.2:
C FILE: 4s
C++ Streams: 6s
MSVC9.0:
C FILE: 10s
C++ Streams: 23s
(note, the MSVC version was run on my laptop with a significantly slower harddrive)
But this gives us a performance difference of 1.5-2.3x, depending on the implementation. and other external factors.