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;

You should check that CreateFile returns success although you are presumably getting a sensible value returned from GetFileSize.

You should check that ReadBuffer has been successfully allocated before trying to use it.

You shouldn't pass a buffer size of BufferSize+1 to ReadFile when ReadBuffer is in fact only BufferSize in length, either allocate more memory or reduce the size passed to the call.

If ReadFile is failing you should be calling GetLastError to find out what the actual error is.

However your actual problem is probably that you are trying to print as text a buffer of binary data in cout << ReadBuffer; .

That works if ReadBuffer contains text but since the nul characater '\0' is used as the end marker for text and a binary file is likely to contain lots of nul characters it only prints the file up to the first nul. You need to output the individual character values of your buffer something like cout << hex << int(ReadBuffer[0]); for the first byte in the buffer.

I understand that the problem is that is stops reading the file when it hits a NULL character.

cout << hex << int(ReadBuffer[0]);

This will not work as it's not the output that is the problem, ReadFile is only reading a file to buffer up to a NULL character.

Is there a way to make ReadFile read past the null character?

How do you know it is only reading up to the NULL?

How do you know it is only reading up to the NULL?

because when i save the buffer to a file I only get up to the null character.

And how do you know that it is the code that reads the file that is wrong and not your code that writes the new file?

I see nothing in the posted code that suggests that it would not be reading the entire file only code that fails to handle the data read correctly.

And how do you know that it is the code that reads the file that is wrong and not your code that writes the new file?

I see nothing in the posted code that suggests that it would not be reading the entire file only code that fails to handle the data read correctly.

Sorry, you are correct. As you rightly said the problem was not reading the file, it is what I was doing with it aftwerwards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.