ManticMoo.COM All Articles Jeff's Articles
Jeffrey P. Bigham

ReadFile: Reading data into a buffer from a file

Jeffrey P. Bigham

Related Ads

Reading data into a buffer using the Windows crazy API had me tripped up for a while, but I eventually got it figure out. Here's a simple example of reading binary data into an array using the CreateFile and ReadFile routines. These were prepared using Visual C++ and Visual Studio 8.0.


HANDLE hFile =
  CreateFile("C:\\Temp\\test.jpg",
             GENERIC_READ,
             NULL,
             NULL,
             OPEN_EXISTING,
             FILE_ATTRIBUTE_NORMAL,
             NULL);
  
  DWORD num_to_read = GetFileSize(hFile,NULL);

  DWORD num_read = -1;

  byte *bytes = new byte[num_to_read];
  if(!ReadFile(hFile,bytes,num_to_read,&num_read,NULL)) {
    printf("Error.\n");
  }

  CloseHandle(hFile);

Other Applicable search terms

  • I/O with Visual C++ 8.0
  • iostream and Visual C++ 8.0
  • Reading data into a buffer "Visual Studio"
  • Jeffrey P. Bigham
    ManticMoo.COM All Articles Jeff's Articles