如何获得文件的地图视图只有ifstream
How to get map view of file just have only ifstream?
我需要创建文件映射,并为我的ifstream获取文件的映射视图。
HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, 0);
if (hFileMap != NULL)
{
BYTE *pData = (BYTE *)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, dwFileSize);
if (pData != NULL)
{
fillDllInfo(pData, dwFileSize);
UnmapViewOfFile(pData);
}
我在这里创建文件,等等。但我需要做一些类似的事情
std::ifstream pefile;
pefile.open(this->fileName, std::ios::in | std::ios::binary);
if(!pefile.is_open())
return error(erId::Cant_Open_File);
std::streamoff filesize = pefile.tellg();
//HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, 0);
//if (hFileMap != NULL)
//{
// BYTE *pData = (BYTE *)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, dwFileSize);
// if (pData != NULL)
//{
fillDllInfo(pData /* ??? */, filesize );
// UnmapViewOfFile(pData);
//}
和我的pefile一起工作!在没有boost或其他任何东西的情况下,我如何在我的pefile上获取pData?谢谢
您可以将所有文件加载到缓冲区中,而不是映射内存中的文件。它的效率要低得多,但它是便携的。
#include <string>
#include <fstream>
#include <cstring> // memcpy
void fillDllInfo (char* buf, size_t size)
{
// ...
}
char* read_whole_stream (std::ifstream& stream, size_t& size)
{
std::istreambuf_iterator<char> eos;
std::string s (std::istreambuf_iterator<char>(stream), eos);
char* buf = new char[s.size()];
memcpy(buf, s.c_str(), s.size());
return buf;
}
int main ()
{
std::ifstream pefile;
pefile.open("in", std::ios::in | std::ios::binary);
if (!pefile.is_open())
return 1;
size_t filesize;
char* pData = read_whole_stream(pefile, filesize);
fillDllInfo(pData, filesize);
delete[] pData;
return 0;
}
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- ifstream什么都没读
- 视图中的参数推导失败:take_while
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在另一个类视图中添加最多2个图表的正确方法是什么
- ifstream文件在从行中读取时被清除
- OpenVR:向视图方向移动
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- 将所选值(通过视图)从 boost::multi_array 复制到另一个数组 (C++)
- 如何在不使用滚动条的情况下使视图更改
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 列表视图更改选择颜色
- 将 ifstream 拆分为 n 个流?
- 如何将 std::ifstream 转换为 std::basic_istream<CharT, Traits>&?
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 是否可以在没有真实文件的情况下创建 ifstream
- 在 Qml 中从 QSqlTableModel 中删除单行时视图不一致
- 如何获得文件的地图视图只有ifstream