用C++内存映射,从超大的数据文件中查找某个指定的字符串key,并将文件中包含key的行的数据都显示在界面上 100
HANDLEhFile=CreateFile(m_FilePath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FIL...
HANDLE hFile = CreateFile(m_FilePath, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
TRACE("Error:%drn", GetLastError());
return;
}
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hFileMap == NULL)
{
TRACE("Error:%drn", GetLastError());
return;
}
// 得到系统分配粒度
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;//一般是65536(byte)=64KB
DWORD dwFileSizeHigh;
__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh);
qwFileSize |= (((__int64)dwFileSizeHigh) << 32);
CloseHandle(hFile);
__int64 qwFileOffset = 0;
DWORD dwBlockBytes = 1000 * dwGran;
if (qwFileSize < 1000 * dwGran)
dwBlockBytes = (DWORD)qwFileSize;
while (qwFileOffset >= 0)
{
LPBYTE lpbMapAddress = (LPBYTE)MapViewOfFile(hFileMap,FILE_MAP_ALL_ACCESS,
(DWORD)(qwFileOffset >> 32), (DWORD)(qwFileOffset & 0xFFFFFFFF),
dwBlockBytes);
if (lpbMapAddress == NULL)
{
TRACE("Error:%drn", GetLastError());
return;
}
//该函数如何书写呢?从lpbMapAddress所指向的内存地址中搜索所有给定的关键字:keyword
//并返回关键字keyword所在那一行的数据
UnmapViewOfFile(lpbMapAddress);
qwFileOffset += dwBlockBytes;
qwFileSize -= dwBlockBytes;
}
CloseHandle(hFileMap); 展开
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
TRACE("Error:%drn", GetLastError());
return;
}
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hFileMap == NULL)
{
TRACE("Error:%drn", GetLastError());
return;
}
// 得到系统分配粒度
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;//一般是65536(byte)=64KB
DWORD dwFileSizeHigh;
__int64 qwFileSize = GetFileSize(hFile, &dwFileSizeHigh);
qwFileSize |= (((__int64)dwFileSizeHigh) << 32);
CloseHandle(hFile);
__int64 qwFileOffset = 0;
DWORD dwBlockBytes = 1000 * dwGran;
if (qwFileSize < 1000 * dwGran)
dwBlockBytes = (DWORD)qwFileSize;
while (qwFileOffset >= 0)
{
LPBYTE lpbMapAddress = (LPBYTE)MapViewOfFile(hFileMap,FILE_MAP_ALL_ACCESS,
(DWORD)(qwFileOffset >> 32), (DWORD)(qwFileOffset & 0xFFFFFFFF),
dwBlockBytes);
if (lpbMapAddress == NULL)
{
TRACE("Error:%drn", GetLastError());
return;
}
//该函数如何书写呢?从lpbMapAddress所指向的内存地址中搜索所有给定的关键字:keyword
//并返回关键字keyword所在那一行的数据
UnmapViewOfFile(lpbMapAddress);
qwFileOffset += dwBlockBytes;
qwFileSize -= dwBlockBytes;
}
CloseHandle(hFileMap); 展开
2015-04-22
展开全部
然后用mapping弄。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-04-22
展开全部
既然要指定行了,那不给hint优化的随机访问映射也没有多少用了,照样得从头读。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询