C++ and C file I/O

后端 未结 9 1549
你的背包
你的背包 2020-12-05 03:54

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

相关标签:
9条回答
  • 2020-12-05 04:22

    Lots. Drawbacks too. See C++ language FAQ for details. In short: type-safety and user-defined types.

    0 讨论(0)
  • 2020-12-05 04:23

    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.

    0 讨论(0)
  • 2020-12-05 04:25

    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.

    0 讨论(0)
提交回复
热议问题