Saturday, January 14, 2012

Binary Files

Written by Erez Wenger - January 2012

After knowing the basics of file handling in C language, handling Binary files can be very usefull.

What is a Binary File?

A binary file is a type of file that is widely used in programming and is common especially in dealing with large amount of data like in instrumentation measuring, the best way for understanding it, is by the difference between a "regular" text file and a binary one:
  • A text file is actually a binary file that has each of it's characters written in ASCII code, for example the letter 'A' has the ASCII value of 65 at decimal base and 01000001 at a binary base, what is actually written in the file is the code in the binary base which has 8 bits or 1 byte of data. By opening the file in a document or text viewer (like Notepad), the binary numbers will be converted to ASCII and to characters which will be displayed as text on the screen.
  • A binary file has every type of data in it with a binary representation, for example if we write a short int (2 bytes of data) of the number 3 it will look like - 00000000 00000011. Opening a binary file with a text viewer will convert the binary numbers into ASCII characters and the result will look like gibberish on screen (since the numbers do not represent ASCII at all).
For example, let's see how the number 10000 is written in both types of files:
  • In a text file each of the digits of the number will be convereted to a binary representation of it's ASCII code - 01100001 01100000 01100000 01100000 01100000.
  • In a binary file, the representation will be much more concise: the number, when written as a short int, will look like this - 00100111 00010000.
Choosing Between Binary or Text
  • A text file can be viewed easily with a simple text viewer but it's not 'economic' because each of the digits or characters will be written in 1 byte - A total waste of space.
  • A binary file is more size efficient but can only be viewed with special viewers or deciphered with your own code format.
  • The rule for choosing between the two types is if you have a large mass of data and you want to save storage size use binary, otherwise use a simple text file.

Code Example - Reading a Binary File

fp=fopen("myfilename.dat","rb"); // open the binary file for writing

fseek (myfile , 10*sizeof(short) , SEEK_SET ); 
// go to the 10th short int from the beggining of the file

fread(&d,sizeof(short),1,fp); // read a short int and stores it into the variable d

fclose(fp); // close the file and save it

Code Example - Writing to a Binary File

fp=fopen("myfilename.dat","wb"); // open the binary file for writing


fwrite (&d, sizeof(short), 1,fp); // writes the value of the d short variable to the file

fclose(fp); // close the file and save it

No comments:

Post a Comment