//-------------------------------------------------------------------
//-- Read File C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//-- you must not claim that you wrote the original code.
//-- If you use this code, an acknowledgment in the
//-- documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-- To use this sample, you must include the following headers
//-------------------------------------------------------------------
#include <windows.h>
#include <tchar.h>
//-------------------------------------------------------------------
//-- Function: readFile
//-- Arguments: TCHAR *filePath - path to the file to read
//-- unsigned long * ulError - in/out check for any errors
//-- Returns: void * - pointer to the fileData, if NULL check ulError
//-- Description: reads the data contained in the file opened by the
//-- class and stores that data in a class variable
//-------------------------------------------------------------------
void *readFile ( TCHAR *filePath, unsigned long *ulError )
{
// set up our return buffer and filesize holder
void *fileData = NULL;
unsigned long ulFileSize = 0;
// reset our error code to 0 - success
*ulError = 0;
// try to open the file for reading
HANDLE hFile = CreateFile ( filePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// see if we were able to open the file
if ( hFile != INVALID_HANDLE_VALUE )
{
// see how big the file is - so we can allocate enough memory
ulFileSize = GetFileSize ( hFile, NULL );
// see if we have a valid file size
if ( ulFileSize != 0xFFFFFFFF )
{
// allocate a chunk of virtual memory to shove the
// file data into
fileData = VirtualAlloc ( NULL,
ulFileSize,
MEM_COMMIT,
PAGE_READWRITE );
// were we able to get some memory
if ( fileData != NULL )
{
// make sure the memory is completely empty
ZeroMemory ( fileData, ulFileSize );
// now try to read the file
if ( !ReadFile ( hFile,
fileData,
ulFileSize,
&ulBytes,
NULL))
*ulError = GetLastError ( );
}
else
*ulError = E_OUTOF_MEMORY;
}
else
// returning a generic error code for an invalid
// file size - you should a custom error code for this
*ulError = 5;
// close the file
CloseHandle ( hFile);
}
else
*ulError = GetLastError ( );
// return either NULL on failure or a pointer to the file data
return fileData;
}
//-- Read File C++ sample - Copyright © 2006, Dave Moats
//--
//-- This sample is provided 'as-is', without any express or implied warranty.
//-- In no event will the authors be held liable for any damages arising from
//-- the use of this sample code.
//--
//-- Permission is granted to anyone to use this sample code for any purpose,
//-- including commercial applications, subject to the following restrictions:
//--
//-- The origin of this code must not be misrepresented;
//-- you must not claim that you wrote the original code.
//-- If you use this code, an acknowledgment in the
//-- documentation is requested - shown below:
//--
//-- Portions Copyright © 2006, Dave Moats (http://www.davemoats.com/).
//--
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-- To use this sample, you must include the following headers
//-------------------------------------------------------------------
#include <windows.h>
#include <tchar.h>
//-------------------------------------------------------------------
//-- Function: readFile
//-- Arguments: TCHAR *filePath - path to the file to read
//-- unsigned long * ulError - in/out check for any errors
//-- Returns: void * - pointer to the fileData, if NULL check ulError
//-- Description: reads the data contained in the file opened by the
//-- class and stores that data in a class variable
//-------------------------------------------------------------------
void *readFile ( TCHAR *filePath, unsigned long *ulError )
{
// set up our return buffer and filesize holder
void *fileData = NULL;
unsigned long ulFileSize = 0;
// reset our error code to 0 - success
*ulError = 0;
// try to open the file for reading
HANDLE hFile = CreateFile ( filePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// see if we were able to open the file
if ( hFile != INVALID_HANDLE_VALUE )
{
// see how big the file is - so we can allocate enough memory
ulFileSize = GetFileSize ( hFile, NULL );
// see if we have a valid file size
if ( ulFileSize != 0xFFFFFFFF )
{
// allocate a chunk of virtual memory to shove the
// file data into
fileData = VirtualAlloc ( NULL,
ulFileSize,
MEM_COMMIT,
PAGE_READWRITE );
// were we able to get some memory
if ( fileData != NULL )
{
// make sure the memory is completely empty
ZeroMemory ( fileData, ulFileSize );
// now try to read the file
if ( !ReadFile ( hFile,
fileData,
ulFileSize,
&ulBytes,
NULL))
*ulError = GetLastError ( );
}
else
*ulError = E_OUTOF_MEMORY;
}
else
// returning a generic error code for an invalid
// file size - you should a custom error code for this
*ulError = 5;
// close the file
CloseHandle ( hFile);
}
else
*ulError = GetLastError ( );
// return either NULL on failure or a pointer to the file data
return fileData;
}
Copyright © 2010 Dave Moats. All rights reserved. Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED. Void where prohibited by law. Please report any issues or broken links.
You may link to this site freely from your own site. You may quote from this site, but please include a link to the original source on the originating site.