Search code examples
cwinapireadfile

WinAPI ReadFile stops when reaching null


I'm trying to write a very simple win32 program. I open a file for reading using CreateFile(), and then read it's content using ReadFile()

HANDLE hfile=CreatFileW(L"Capturejpg.jpg", GENERIC_READ, 0,
              NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
bReadResult=ReadFile(hFile, ReadBuff, BUFFERSIZE-1, &dwBytes, &OL);

when I'm reading a .txt file for example, this works just fine, the problem is when the file I am reading contains NULL (which is the case with most files) then ReadFile function stops reading(or maybe writing bytes to ReadBuff) when reaching the first NULL. before asking I searched and found two answers.

  1. use something other that char array and char *: for this, I don't know what else I can use, cause my goal is to read the file and search for the files extension(for example if it's .gif then the first 3 characters read "Gif")
  2. change DCB: the problem with this one is that I have no idea what DCB is actually, how to change it, and change exactly what in it.

EDIT: other posts with the same problem: this one and this one


Solution

  • ReadFile does not care one bit about the content that it reads. It will quite happily read zero bytes and continue reading beyond that point in the file. It wouldn't be much use if it could not do that.

    You have just misdiagnosed the problem. You have read into a character array ReadBuffer and then printed like this:

    printf("%s", ReadBuffer);
    

    Now, printf will indeed stop when it reaches a zero byte. You will need to find some other way to output the content of this file.