C++ 和 C 文件 I/O [英] C++ and C file I/O

查看:22
本文介绍了C++ 和 C 文件 I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 文件 I/O 比 C 文件 I/O 更难.那么在 C++ 中,为文件 I/O 创建一个新的库是否有用?我的意思是 谁能告诉我 C++ 文件 I/O 有什么好处吗?

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 <fstream> Can anyone please tell are there any benefits in C++ file I/O ?

推荐答案

意见

我不知道有任何使用 C++ 流的真实项目.它们太慢且难以使用.有几个较新的库,例如 FastFormatBoost 声称更好的版本在上一期 ACCU Overload 杂志中有一篇关于它们的文章.就我个人而言,我在过去 15 年左右的时间里一直在 C++ 中使用 c FILE 库,我看不出有任何改变的理由.

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.

速度

这里是一个小测试程序(我很快就凑齐了)来展示基本的速度问题:

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
", teststr, i );
        }
    fclose( file );
    int end = time(0);

    printf( "C FILE: %ds
", 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
", end-start );

    return 0;
    }

以及我电脑上的结果:

C FILE: 5s
C++ Streams: 260s

Process returned 0 (0x0)   execution time : 265.282 s
Press any key to continue.

正如我们所看到的,这个简单的例子慢了 52 倍.我希望有办法让它更快!

As we can see just this simple example is 52x slower. I hope that there are ways to make it faster!

注意: 在我的示例中将 endl 更改为 ' ' 改进了 C++ 流,使其仅比 FILE* 流慢 3 倍(感谢 jalf) 可能有办法让它更快.

NOTE: changing endl to ' ' 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.

使用困难

我不能说 printf() 并不简洁,但一旦您通过宏代码的初始 WTF,它就会更灵活 (IMO) 并且更易于理解.

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 << '
';
printf( "%.5f
", pi );

cout << "pi = " << fixed << showpos << setprecision(3) << pi << '
'; 
printf( "%+.3f
", pi );

cout << "pi = " << scientific << noshowpos << pi<< '
';
printf( "%e
", pi );

问题

是的,可能需要更好的 C++ 库,很多是 FastFormat 就是那个库,只有时间会证明一切.

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

戴夫

这篇关于C++ 和 C 文件 I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆