ReadFile fails

Hello all.
I'm trying to read file in chunks using Windows API ReadFile() but it fails for some reason when hexadecimal value "00" is met in process of reading file. The buffer who receive data from file is defined as char *buffer = new char[(buffSize)]{};

Anyone know why this happens ?

Regards.


How can anyone say without reading the relevant code?
Fails in what way?
if ReadFille fails call GetLastError() to see what went wrong.
Problem is GetLastError() doesnt return anything, since ReadFile() dont fail, it just dont read data which comes after "00" which is null byte in file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	
HANDLE fCreate;
DWORD  bytesRead;


	fCreate = CreateFile(fPath, GENERIC_READ, FILE_SHARE_READ, NULL, 3, FILE_ATTRIBUTE_NORMAL, NULL);

	char   *buffer = new char[(500 + 1)]{};

	if (!fCreate)
		buffer = "Failed to open file!";

	SetFilePointer(fCreate, 0, NULL, FILE_BEGIN);

	while(ReadFile(fCreate, buffer, 500, &bytesRead, NULL)) {

              // Check is EOF reached
		if (bytesRead == 0) {
			delete[] buffer;
			break;
		}

		MessageBox(NULL, buffer, "BUFFER", MB_OK);
	}
ReadFile should read to complete file since it doesn't car about 0.
The problem seems rather that MessageBox will see the 00 as the end of the string;
An idea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  const int BUF_SIZE = 501;
  char buffer[BUF_SIZE];
  if (ReadFile(fCreate, buffer, BUF_SIZE - 1, &bytesRead, NULL))
  {
    for (int i = 0; i < BUF_SIZE - 1; i++)
    {
      if (buffer[i] == '\0')
      {
        buffer[i] = ' '; // or sth.else
      }
    }
    buffer[BUF_SIZE - 1] = '\0';
  }
  MessageBox(NULL, buffer, "BUFFER", MB_OK);

> it just dont read data which comes after "00" which is null byte in file.

It reads the data in the file. The MessageBox function which expects a c-style string treats the zero byte as the terminating null character and ignores that and everything after that.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <windows.h>
#include <vector>
#include <iomanip>
#include <sstream>

int main()
{
    const char* fpath = __FILE__ ; // replace with the desired path

    const auto file = ::CreateFile( fpath, GENERIC_READ, FILE_SHARE_READ, nullptr,
                                    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr ) ;
    if( file != INVALID_HANDLE_VALUE )
    {
         std::vector<char> buffer(500) ;
         DWORD nbytes_read ;
         while( ReadFile( file, buffer.data(), buffer.size(), &nbytes_read, nullptr ) &&
                nbytes_read > 0 )
         {
             std::ostringstream stm ;

             stm << "read " << std::dec << nbytes_read << " bytes. values (hex) are:\n\n"
                 << std::hex << std::setfill('0') ;
             for( DWORD i = 0 ; i < nbytes_read ; ++i )
             {
                 stm << std::setw(2) << int( buffer[i] ) << ' ' ;
                 if( i%25 == 24 ) stm << '\n' ;
             }

             MessageBox( nullptr, stm.str().c_str(), "BUFFER", MB_OK ) ;
         }
    }
}
@Thomas1965 solution worked perfectly, many thanks!
Topic archived. No new replies allowed.