當我嘗試打開「.EXE」文件與ReadFile()
的Windows API,它只是返回,如文件的2第一個字符:MZ
讀出「二進制」與ReadFile的WinAPI的文件
這裏是我的代碼:
#define BUFFERSIZE 5000
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped
);
VOID CALLBACK FileIOCompletionRoutine(
__in DWORD dwErrorCode,
__in DWORD dwNumberOfBytesTransfered,
__in LPOVERLAPPED lpOverlapped)
{
_tprintf(TEXT("Error code:\t%x\n"), dwErrorCode);
_tprintf(TEXT("Number of bytes:\t%x\n"), dwNumberOfBytesTransfered);
g_BytesTransferred = dwNumberOfBytesTransfered;
}
HANDLE hFile;
DWORD dwBytesRead = 0;
char ReadBuffer[BUFFERSIZE] = { 0 };
OVERLAPPED ol = { 0 };
hFile = CreateFile(fullFilePath.c_str(), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template
ReadFileEx(hFile, ReadBuffer, BUFFERSIZE - 1, &ol, FileIOCompletionRoutine);
當我打印ReadBuffer
這只是MZ
(exe文件)。
但使用:
std::ifstream file(argv[1], std::ios::in | std::ios::binary);
它的工作完美。 如何使用ReadFile讀取二進制文件?
你怎麼打印'ReadBuffer'?作爲NUL終止的字符串,我懷疑。當然不是。 –
@IgorTandetnik我把'ReadBuffer'的值放到String中,並用'std :: cout'打印字符串。 –
準確地說。您將它打印爲文本數據,但它是二進制數據。它可能在'MZ'後面有一個零字節,並且打印在那裏停止。 –