Binary Files C

  • Comparison(Text Vs Binary)
  • Block I/O using binary mode

Now will will study binary files and see in what way text files differ from binary files also we will see how to transfer data blocks to and from files as a whole.

Comparison (Text Vs Binary)

  • How they handle newline characters.
  • How do they indicate end of file.
  • How they store numbers on disk.

They are mainly three differences between text mode and binary mode files; we will discuss each in detail.
For binary File I/O you use fread and fwrite.

The declarations for each are similar:

Both of these functions deal with blocks of memories - usually arrays. Because they accept pointers, you can also use these functions with other data structures; you can even write structs to a file or a read struct into memory.

Let's look at one function to see how the notation works.

fread takes four arguments. Don't by confused by the declaration of a void *ptr; void means that it is a pointer that can be used for any type variable. The first argument is the name of the array or the address of the structure you want to write to the file. The second argument is the size of each element of the array; it is in bytes. For example, if you have an array of characters, you would want to read it in one byte chunks, so size_of_elements is one. You can use the sizeof operator to get the size of the various datatypes; for example, if you have a variable int x; you can get the size of x with sizeof(x);. This usage works even for structs or arrays. Eg, if you have a variable of a struct type with the name a_struct, you can use sizeof(a_struct) to find out how much memory it is taking up.



The third argument is simply how many elements you want to read or write; for example, if you pass a 100 element array, you want to read no more than 100 elements, so you pass in 100.

The final argument is simply the file pointer we've been using. When fread is used, after being passed an array, fread will read from the file until it has filled the array, and it will return the number of elements actually read. If the file, for example, is only 30 bytes, but you try to read 100 bytes, it will return that it read 30 bytes. To check to ensure the end of file was reached, use the feof function, which accepts a FILE pointer and returns true if the end of the file has been reached.

fwrite() is similar in usage, except instead of reading into the memory you write from memory into a file.

For example,

How they handle newline characters
‘C’ considers newline as a single character ‘ ’. On the other hand, DOS converts it into two characters linefeed( ) and newline( ).
Thus when a file is opened in text mode and a ‘C’ program writes a ‘C’ text file to disk DOS converts all newline into carriage return – linefeed combination. And when a ‘C” program reads in a text file, the carriage return linefeed combination are converted to a single newline character.

When number of character in a file are counted, then the count will be less than the number of characters (i.e. filesize) counted by DOS.
Such a discrepancy does not arise when file is opened in binary mode, because these conversions will not take place. And the file size will go hand in hand with the DOS operating system.

Till now we have seen how to open file in text mode. For opening a file in binary mode, only a ‘b’ is to be appended to the file open mode.

For text mode we just write “r” or “rt”, since text mode is the default mode.

How do they indicate end of file ?
In text mode, a special character whose ASCII value is 26 is automatically inserted at the end, to indicate end of file. One can read a file till this mark is obtained to read the entire file.
But in case of binary mode, there is no end marker. The directory is used to get number of characters present in the file and this count is used.

One important thing to be noted is that, if a file is written by opening in binary mode and we store a number 26(Hex 1A) in it and then we text mode to read the file, file will be terminated before hand since it will encounter its special character 26 ahead of actual end of file. Thus it is seen that binary and text modes are not compatible. A file written in binary mode should be read in binary mode while a file written in text mode should be read in text mode.

How they store numbers on disk
How are characters and number stored in memory?
A character occupies 1 byte
An integer occupies 2 bytes.
A float occupies 4 bytes. And so on.
When we write numbers using text mode (using fprintf), they are stored as string of characters. i.e. If we stores an integer 1234, in memory, it will occupy 2 bytes. While in a file it would occupy 4 bytes i.e. one byte per character.

Similarly if we have a floating point number 1234.56, in memory, it will occupy 4 bytes. Written to file it will occupy 7 bytes.

Thus number of bytes are equal to number of digits in the number. And as number of digits increase, space required increases.

When we use binary mode, fwrite function stores the numbers in binary format. Binary format means each number occupies same number of bytes on disk as it occupies in memory. i.e. 1234 will occupy 2 bytes on disk while 1234.56 will occupy 4 bytes on disk with binary mode.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics