使用crc校验方法 校验文件

static unsigned long Crc32Table[256];


int Get_CRC( LPCVOID pData, DWORD dwSize)//获取crc32值
{
ULONG  crc(0xffffffff);
int len;
unsigned char* buffer;
len = dwSize;
buffer =( unsigned char* )pData;
while(len--)
crc = (crc >> 8) ^ Crc32Table[(crc & 0xFF) ^ *buffer++];
return crc^0xffffffff;
}


int GetFileCRC(std::string szFileName)
{
HANDLE hFile = {NULL};
DWORD dwSize, bytes_read;


hFile = CreateFile(szFileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);


// 如果该文件不存在
if( hFile == NULL || hFile == INVALID_HANDLE_VALUE )
return (-1);


dwSize = GetFileSize(hFile, NULL);
char *autoptr = new char[dwSize] ;

ReadFile(hFile, autoptr, dwSize, &bytes_read, NULL);
CloseHandle(hFile);



int ncrc =  Get_CRC(autoptr, dwSize);
delete [] autoptr;
return ncrc;
}

你可能感兴趣的:(c++学习总结)