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

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

问题描述

C ++文件I / O比C文件I / O强。
那么在C ++中,为文件I / O创建一个新的库是有用的还是不?我的意思是< fstream>
任何人都可以告诉C ++文件I / O有什么好处吗?

解决方案

意见



我不知道有什么真正的项目使用C ++流。他们太慢,很难使用。有几个较新的库,如 FastFormat Boost 版本声称更好在最后一个ACCU Overload杂志上有一篇关于他们。个人而言,我在C ++中使用了c FILE库,过去15年左右,我看不到有任何改变的理由。



速度



这里是小测试程序(我一起敲快)显示基本速度问题:

  #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 {
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 {
outdata<测试< :<< i<< endl;
}
outdata.close();
end = time(0);

printf(C ++ Streams:%ds \\\
,end-start);

return 0;
}

我的电脑上的结果:

  C文件:5s 
C ++流:260s

进程返回0(0x0)执行时间:265.282 s
按任意键继续。

我们可以看到,这个简单的例子是52x更慢。



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



难以使用



我不能认为printf()不是简洁,

  double pi = 

3.14285714;

cout<< pi =<< setprecision(5)<< pi<< '\\\
';
printf(%.5f \\\
,pi);

cout<< pi =<<固定<展示< setprecision(3)<< pi<< '\\\
';
printf(%+。3f \\\
,pi);

cout<< pi =<<科学< noshowpos< pi<< '\\\
';
printf(%e \\\
,pi);

问题



是的,可能需要一个更好的C ++库,许多都是 FastFormat 是图书馆,只有时间会告诉。 / p>

dave


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 ?

解决方案

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

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

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