文件数据读取内存GetBufferFromFile
2022-01-16
40
0
使用std::string存储从文件中读取的二进制数。
BOOL GetBufferFromFile(TCHAR* pFileName, std::string& strFileStream)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
BOOL isok = FALSE;
do
{
hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
break;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == 0)
{
break;
}
strFileStream.resize(dwFileSize);
DWORD dwBytesRead = 0;
isok = ReadFile(hFile, (PVOID)&strFileStream.at(0), dwFileSize, &dwBytesRead, NULL);
if (!isok)
{
break;
}
} while (0);
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
}
return isok;
}