

#include <stdio.h>                     // Need this for fopen(..) fwrite(..) etc

// We use this simple function to write data to our file.
void fileout(char *str)
{
      FILE *fp;
      fp=fopen("output.txt", "a+");   // a+ -> if file doesn't exist create it, else
      fprintf(fp, "%s", str);         //       append to the end of the file
      fclose(fp); 
}// End fileout(..)

long FileSize( char * szFileName )
{
	FILE *file = fopen(szFileName, "rb"); // Open the file
	fseek(file, 0, SEEK_END);             // Seek to the end
	long file_size = ftell(file);         // Get the current position
	fclose(file);
	
	return file_size;                     // We have the file size, so return it
}// End FileSize(..)

//Program Entry Point
void main(int argc, char* argv[])
{
	char szFileName[] = "default.xbe";    // Our input xbe
	char buf[500];                        // Large temp char buffer

	long iFileSize = FileSize(szFileName);

	sprintf(buf, "File: %s - xbe filesize: %d bytes\n", szFileName, iFileSize);
	fileout(buf);

	// Lets allocate enough memory for the whole xbe and read it all in
	unsigned char * pXBE = new unsigned char[iFileSize];

	// Open our xbe file
	FILE* fp = fopen( szFileName, "r" );
	fseek(fp, 0, SEEK_SET);

	// Read all the contents into our allocated memory 
	/*
	do 
	{
		fread(pXBE, 1, 1, fp);
	} while (!feof(fp));
	*/
	
	// For some reason I had a problem with the above do/while loop..as it
	// didn't seem to read in all the data?  ..hmmm or was aligned wrong
	// so for a quick fix, I chose this instead :)
	fread(pXBE, iFileSize, 1, fp);

	// Close our file.
	fclose( fp );


	// Write out the first 3 char's of the xbe to see what they are
	sprintf(buf, "pXBE[0..3] = %c%c%c%c", pXBE[0], pXBE[1], pXBE[2], pXBE[3] );
	fileout(buf);

	// Remember, before exiting the program, release the memory we allcoated for 
	// the xbe data we read in
	delete[] pXBE;

}// End main(..)






