C++代码在Windows 7中运行,但不在Windows 10中运行

C++ code is running in Windows 7 but not in windows 10

本文关键字:运行 Windows 代码 C++      更新时间:2023-10-16

我有一个在Windows 7中运行的C ++代码,但在Windows10中不起作用。它正在MAC/LINUX中工作。我正在尝试解析一个大的十六进制文件。我的代码加载到数组中,然后应用业务逻辑来生成 csv。文件大小为 2.38GB。下面是代码。

bool readFile (string filename, char ** buffer  ,unsigned  int & sizeOfFile )
{
  ifstream inFile (filename.c_str (), ios::in | ios::binary);
  if (!inFile)
    return false;
  inFile.seekg (0, ios::end);
  size_t size = inFile.tellg ();
  inFile.seekg (0, ios::beg);
  *buffer = new char[size];
  cout<<"n Length of the ARRAY= "<<size;
  inFile.read (*buffer, size);
  inFile.close ();
  sizeOfFile =  size;
  cout<<"File successfully read Press Any Key to Continue.. "<<endl;
  //getch();
  return true;
}

当我在 Visual Studio 2015 下的 Windows 10 以及 dev c++ 下执行文件时,它无法将文件加载到数组中。它在窗口 7 中完美运行。

使用文件映射不是更容易吗?

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366556(v=vs.85(.aspx

编辑:您在下面提到您在macOS和Linux上使用相同的代码。我更熟悉这些平台,我会使用 mmap((。实际上,这可以自动将数据从文件加载到内存。

否则,如上所述,您需要提供有关问题性质(32 位或 64 位内部版本、故障类型(的更多信息。

size_t size = inFile.tellg ();
...
*buffer = new char[size];

看起来您没有为整个文件创建足够大的空间来读取。如果文件大小为 10 字节,则需要分配 11 个字节,因为以 null 结尾的字符串需要在末尾添加一个额外的字符来存储要终止它的''字符。它在某些系统上工作,但不能在其他系统上工作的事实纯粹是运气,因为这意味着在这些系统上,内存中的正确点恰好有一个 NUL 字符来正确终止字符串。