• <small id='GAS8J'></small><noframes id='GAS8J'>

    1. <i id='GAS8J'><tr id='GAS8J'><dt id='GAS8J'><q id='GAS8J'><span id='GAS8J'><b id='GAS8J'><form id='GAS8J'><ins id='GAS8J'></ins><ul id='GAS8J'></ul><sub id='GAS8J'></sub></form><legend id='GAS8J'></legend><bdo id='GAS8J'><pre id='GAS8J'><center id='GAS8J'></center></pre></bdo></b><th id='GAS8J'></th></span></q></dt></tr></i><div id='GAS8J'><tfoot id='GAS8J'></tfoot><dl id='GAS8J'><fieldset id='GAS8J'></fieldset></dl></div>
      <tfoot id='GAS8J'></tfoot>

    2. <legend id='GAS8J'><style id='GAS8J'><dir id='GAS8J'><q id='GAS8J'></q></dir></style></legend>

        <bdo id='GAS8J'></bdo><ul id='GAS8J'></ul>

      1. C++ 和 C 文件 I/O

        C++ and C file I/O(C++ 和 C 文件 I/O)

        <i id='meZTQ'><tr id='meZTQ'><dt id='meZTQ'><q id='meZTQ'><span id='meZTQ'><b id='meZTQ'><form id='meZTQ'><ins id='meZTQ'></ins><ul id='meZTQ'></ul><sub id='meZTQ'></sub></form><legend id='meZTQ'></legend><bdo id='meZTQ'><pre id='meZTQ'><center id='meZTQ'></center></pre></bdo></b><th id='meZTQ'></th></span></q></dt></tr></i><div id='meZTQ'><tfoot id='meZTQ'></tfoot><dl id='meZTQ'><fieldset id='meZTQ'></fieldset></dl></div>
        <tfoot id='meZTQ'></tfoot>
            • <bdo id='meZTQ'></bdo><ul id='meZTQ'></ul>
            • <small id='meZTQ'></small><noframes id='meZTQ'>

                  <tbody id='meZTQ'></tbody>
                  <legend id='meZTQ'><style id='meZTQ'><dir id='meZTQ'><q id='meZTQ'></q></dir></style></legend>
                  本文介绍了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++ 流的真实项目.它们太慢且难以使用.有几个较新的库,例如 FastFormat 和 Boost 声称更好的版本在上一期 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                • <legend id='Qnmqe'><style id='Qnmqe'><dir id='Qnmqe'><q id='Qnmqe'></q></dir></style></legend>

                    <small id='Qnmqe'></small><noframes id='Qnmqe'>

                      <tbody id='Qnmqe'></tbody>

                      <tfoot id='Qnmqe'></tfoot>

                        <bdo id='Qnmqe'></bdo><ul id='Qnmqe'></ul>

                          • <i id='Qnmqe'><tr id='Qnmqe'><dt id='Qnmqe'><q id='Qnmqe'><span id='Qnmqe'><b id='Qnmqe'><form id='Qnmqe'><ins id='Qnmqe'></ins><ul id='Qnmqe'></ul><sub id='Qnmqe'></sub></form><legend id='Qnmqe'></legend><bdo id='Qnmqe'><pre id='Qnmqe'><center id='Qnmqe'></center></pre></bdo></b><th id='Qnmqe'></th></span></q></dt></tr></i><div id='Qnmqe'><tfoot id='Qnmqe'></tfoot><dl id='Qnmqe'><fieldset id='Qnmqe'></fieldset></dl></div>