Sunday, 12 September 2010

Understand and reading binary files in C

I posted this tutorial quite a while back on, here, so i won't repost it, no point, but i will post the code snippet that i provided with the tutorial. It is mainly targetted and those who wish to read in the binary information contained within a file, for use in emulation for example, and to teach the difference between text files and binary files.


using namespace std;

// An unsigned char can store 1 Bytes (8bits) of data (0-255)
typedef unsigned char BYTE;

// Get the size of a file
long getFileSize(FILE *file)
 long lCurPos, lEndPos;
 lCurPos = ftell(file);
 fseek(file, 0, 2);
 lEndPos = ftell(file);
 fseek(file, lCurPos, 0);
 return lEndPos;

int main()
 const char *filePath = "C:\\Users\\UrName\\Desktop\\testFile.bin"; 
 BYTE *fileBuf;   // Pointer to our buffered data
 FILE *file = NULL;  // File pointer

 // Open the file in binary mode using the "rb" format string
 // This also checks if the file exists and/or can be opened for reading correctly
 if ((file = fopen(filePath, "rb")) == NULL)
  cout << "Could not open specified file" << endl;
  cout << "File opened successfully" << endl;

 // Get the size of the file in bytes
 long fileSize = getFileSize(file);

 // Allocate space in the buffer for the whole file
 fileBuf = new BYTE[fileSize];

 // Read the file in to the buffer
 fread(fileBuf, fileSize, 1, file);

 // Now that we have the entire file buffered, we can take a look at some binary infomation
 // Lets take a look in hexadecimal
 for (int i = 0; i < 100; i++)
  printf("%X ", fileBuf[i]);

        fclose(file);   // Almost forgot this 
 return 0;