【问题标题】:C++ ofstream write does not work under Windows. Works fine under LinuxC++ ofstream write 在 Windows 下不起作用。在 Linux 下运行良好
【发布时间】:2014-01-18 14:53:44
【问题描述】:

以下代码不适用于 Windows 和 GNU C++、VS10、VS12、Intel C++ 14.0。下面的代码在 Linux 和 GNU C++ 4.7、4.8、Intel C++ 14、Open64 5.0 下工作。在内部测试 for-loop 中用 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() 调用之后添加fileOUT &lt;&lt; std::flush
  • 不写 DIMEN 元素,而是写 DIMEN-256。因此,当我使用 (j = 0; j
  • 你所说的“它坏了”是什么意思?你有没有试过在下一个read()之前做fileOUT &lt;&lt; std::flush; fileIN.sync()

标签: c++ linux windows binary ofstream


【解决方案1】:

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

fileOUT << std::flush;

【讨论】:

  • 0x499602D2 谢谢!添加 fileOUT
猜你喜欢
  • 2021-08-29
  • 2014-02-21
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
相关资源
最近更新 更多