Hi
I'm attempting to copy a file to a buffer using the ReadFile Win API. It works fine for text files but for other file types, for example .exe, it only copies the first few bytes. I can't identify the problem.
I would greatly appreciate it if somebody can help.
Thanks
Ricky
My code:
//file handle
HANDLE hFile;
//something to contain the number of bytes read
DWORD dwNumRead = -1;
hFile = CreateFile("C:\\Test.exe", GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//GetFileSize to determine size of buffer for ReadFile
DWORD BufferSize = GetFileSize(hFile,NULL);
cout << BufferSize <<endl;
char* ReadBuffer = new char [BufferSize];// create a new char * to hold the buffer using the filesize
//Read from the file:
if(!ReadFile(hFile, ReadBuffer,BufferSize+1,&dwNumRead,NULL))
{
printf ("ReadFile Fail");
}
//To close the file:
CloseHandle(hFile);
//output buffer
cout << ReadBuffer;