C++ and C file I/O

后端 未结 9 1548
你的背包
你的背包 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:00

    Banishing buffer overruns seems like a big win for C++ to me.

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

    The performance differences between printf()/fwrite style I/O and C++ IO streams formatting are very much implementation dependent. Some implementations (visual C++ for instance), build their IO streams on top of FILE * objects and this tends to increase the run-time complexity of their implementation. Note, however, that there was no particular constraint to implement the library in this fashion.

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

    • Type safety as already stated earlier.
    • Flexibility of implementation. Code can be written to do specific formatting or input to or from a generic ostream or istream object. The application can then invoke this code with any kind of derived stream object. If the code that I have written and tested against a file now needs to be applied to a socket, a serial port, or some other kind of internal stream, you can create a stream implementation specific to that kind of I/O. Extending the C style I/O in this fashion is not even close to possible.
    • Flexibility in locale settings: the C approach of using a single global locale is, in my opinion, seriously flawed. I have experienced cases where I invoked library code (a DLL) that changed the global locale settings underneath my code and completely messed up my output. A C++ stream allows you to imbue() any locale to a stream object.
    0 讨论(0)
  • 2020-12-05 04:12

    Opinion

    I don't know of any real project that uses C++ streams. They are too slow and difficult to use. There are several newer libraries like FastFormat and the Boost version that claim to be better there was a piece in the last ACCU Overload magazine about them. Personally I have used the c FILE library for the last 15 years or so in C++ and I can see no reason yet to change.

    Speed

    Here is small test program (I knock together quickly) to show the basic speed problem:

    #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 just this simple example is 52x slower. I hope that there are ways to make it faster!

    NOTE: changing endl to '\n' in my example improved C++ streams making it only 3x slower than the FILE* streams (thanks jalf) there may be ways to make it faster.

    Difficulty to use

    I can't argue that printf() is not terse but it is more flexible (IMO) and simpler to understand, once you get past the initial WTF for the 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 );
    

    The Question

    Yes, may be there is need of a better C++ library, many be FastFormat is that library, only time will tell.

    dave

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

    Please have a look at

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

    then You will prefer C++ I/O than C I/O.

    in short C is prefered if you know data size prior to read or write and for speed. C++ is prefered if you don't know data size and for efficient code.

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

    std::ifstream and std::ofstream are already in stl library. You don't have to create your own.

    The main benefit is all outputs and inputs are type safety.

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

    Whenever I need to take inputs/give outputs in a file in C++, I just use two lines.

    freopen("input.txt","r",stdin); // for input from file
    freopen("output.txt","w",stdout);// for output from file
    

    Now you can scan variables as you would normally do from console, and whatever you print as output will be displayed in output.txt file.

    So I don't think file I/O in c++ is tough, its rather easier than c.

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