草庐IT

C++ ofstream 写入在 Windows 下不起作用。在 Linux 下工作正常

coder 2024-06-21 原文

以下代码在 Windows 和 GNU C++、VS10、VS12、Intel C++ 14.0 下不起作用。下面的代码在 Linux 和 GNU C++ 4.7、4.8、Intel C++ 14、Open64 5.0 下工作。在内部测试 for 循环中用 DIMEN-256 替换 DIMEN ... 有效!?任何的想法?

//============================//
// Read and Write binary file //
// using buffers              //
//============================//

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
  // 1. variables and parameters

  const long int DIMEN = static_cast<long int>(pow(10.0,8.0));
  const long int I_DO_MAX = 100;
  const string fileName = "my_file.bin";
  ofstream fileOUT;
  ifstream fileIN;
  double* myArrayAlpha = new double [DIMEN];
  double* myArrayBeta = new double [DIMEN];
  long int i;
  long int j;

  // 2. build the array with some data

  cout << " 1 --> Build the array with some data" << endl;

  for (i = 0; i < DIMEN; i++)
  { myArrayAlpha[i] = static_cast<double>(i); }

  for (i = 0; i < I_DO_MAX; i++)
  {
    // 3. open the file stream

    cout << "-------------->>> " << i << endl;
    cout << " 2 --> Open the file stream" << endl;

    fileOUT.open(fileName.c_str(), ios::out | ios::binary | ios::trunc);
    fileIN.open(fileName.c_str(), ios::in | ios::binary); 

    // 4. test if the file stream is opened 

    cout << " 3 --> Test if the file stream is opened with success" << endl;

    if (!fileOUT.is_open())
    { cout << "Error! The output file stream is not opened. Exit." 
           << endl; return -1; }

    if (!fileIN.is_open())
    { cout << "Error! The input file stream is not opened. Exit." 
           << endl; return -1; }

    // 5. write the contents of myArrayAlpha[] to a file

    cout << " 4 --> Write and then Read to the file" << endl;

    fileIN.seekg(0, fileIN.beg);
    fileOUT.seekp(0);

    fileOUT.write(reinterpret_cast<char*>(&myArrayAlpha[0]), 
                  DIMEN * sizeof(double));
    fileIN.read(reinterpret_cast<char*>(&myArrayBeta[0]), 
                DIMEN * sizeof(double));

    // 6. test that I am writting and reading correctly

    for (j = 0; j < DIMEN; j++) // replace DIMEN 
    {                           // with DIMEN-256 to work under Windows
      if (myArrayAlpha[j] != myArrayBeta[j])
      { cout << myArrayAlpha[j] << endl;
        cout << myArrayBeta[j] << endl;
        cout << "j = " << j << endl;
        cout << "Error!"; return -1; }
    }

    cout << " 5 --> Read and Write with success" << endl;
    cout << " 6 --> Close the I/O streams" << endl;

    // 7. close the file stream

    fileIN.close();
    fileOUT.close();
  }

  // 8. free up the RAM

  delete [] myArrayAlpha;
  delete [] myArrayBeta;

  return 0;
}

最佳答案

问题是您的数据在 write 调用后没有被刷新到外部序列,所以它仍然位于内部缓冲区中。在 write() 之后添加这一行:

fileOUT << std::flush;

关于C++ ofstream 写入在 Windows 下不起作用。在 Linux 下工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21205684/

有关C++ ofstream 写入在 Windows 下不起作用。在 Linux 下工作正常的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  5. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  6. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

  7. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  10. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

随机推荐