主题:急!急!急!内存映射文件
我用内存映射文件加快读取大文件的速度,因文件很大,所以我分块映射,结果,读取的数字不正确,为什么?
代码如下:
......
Qt::HANDLE hFile;
Qt::HANDLE hMapFile;
quint8 * lpwFileAddr;
hFile = CreateFile( fileName.utf16() , GENERIC_READ,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
QMessageBox::warning( 0, "aaa1", QString::fromLocal8Bit("创建文件对象失败,错误代码") );
return false;
}
hMapFile= CreateFileMapping(hFile,NULL, PAGE_READONLY,0,0,NULL);
if ( hMapFile==NULL )
{
QMessageBox::warning( 0, "aaa2", QString::fromLocal8Bit("创建文件映射对象失败,错误代码") );
return false;
}
SYSTEM_INFO sinf;
GetSystemInfo(&sinf);
DWORD dwAllocationGranularity = sinf.dwAllocationGranularity;
quint64 offset=dwAllocationGranularity*1024;
quint64 qwFileOffset=0;
lpwFileAddr=(quint8 *)MapViewOfFile(hMapFile,FILE_MAP_READ, 0, qwFileOffset,offset);
if ( lpwFileAddr == NULL )
{
QMessageBox::warning( 0, "aaa3", QString::fromLocal8Bit("创建文件映射对象失败,错误代码") );
return false;
}
memcpy( &recNum, lpwFileAddr +3608, sizeof( Q_INT32 ) );
....
当我打印recNum时,发现它的值为16777216,这个值是不对的,应该是210,不知道为什么?
代码如下:
......
Qt::HANDLE hFile;
Qt::HANDLE hMapFile;
quint8 * lpwFileAddr;
hFile = CreateFile( fileName.utf16() , GENERIC_READ,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
QMessageBox::warning( 0, "aaa1", QString::fromLocal8Bit("创建文件对象失败,错误代码") );
return false;
}
hMapFile= CreateFileMapping(hFile,NULL, PAGE_READONLY,0,0,NULL);
if ( hMapFile==NULL )
{
QMessageBox::warning( 0, "aaa2", QString::fromLocal8Bit("创建文件映射对象失败,错误代码") );
return false;
}
SYSTEM_INFO sinf;
GetSystemInfo(&sinf);
DWORD dwAllocationGranularity = sinf.dwAllocationGranularity;
quint64 offset=dwAllocationGranularity*1024;
quint64 qwFileOffset=0;
lpwFileAddr=(quint8 *)MapViewOfFile(hMapFile,FILE_MAP_READ, 0, qwFileOffset,offset);
if ( lpwFileAddr == NULL )
{
QMessageBox::warning( 0, "aaa3", QString::fromLocal8Bit("创建文件映射对象失败,错误代码") );
return false;
}
memcpy( &recNum, lpwFileAddr +3608, sizeof( Q_INT32 ) );
....
当我打印recNum时,发现它的值为16777216,这个值是不对的,应该是210,不知道为什么?