C++代码在Windows 7中运行,但不在Windows 10中运行
C++ code is running in Windows 7 but not in windows 10
我有一个在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 字符来正确终止字符串。
相关文章:
- 在Windows中以.exe的形式运行c++
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- 函数在Windows或Linux上运行时表现不同
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- 在 Linux 群集上运行 Windows C++代码
- 使用 Qt 运行 Windows PowerShell 命令
- 如何通过C++程序运行Windows命令?
- 如何正确编译和运行Windows表单应用程序
- 在Qt应用程序中运行Windows命令提示符命令
- 在 Windows xp 中运行 Windows 7 C++/Cli 应用程序
- 在windows上运行windows便携式设备应用程序的要求(预安装)是什么
- 在C++中安装并运行Windows服务
- 如何使用Qt运行windows cmd命令
- Qt Creator:如何运行windows批处理文件并获得结果
- 如何判断屏幕保护程序是否在Qt (C/ c++)程序中运行?Windows (MS)
- 传输数据到运行windows 10的树莓派Tx
- 以特权用户身份运行windows服务
- 运行Windows Phone 8应用程序时出现System.IO.FileNotFoundException错误
- 我可以在Surface RT上运行Windows 2003 SDK应用程序吗
- 在后台连续运行windows phone8.1应用程序