What is binary file?

Binary files store data in binary format, and not in plain text format. A binary file is computer readable and not human readable. We can assume them to be similar to arrays of structures, except the structures are in a disk file rather than in an array in memory. Unlike plain text files, in binary files

  • We can jump to a specific location in file. You can consider it similar to accessing via index in an array
  • We can change the content at specific location in file. Once again, it is similar to accessing and array via index and changing the content directly. Whereas in a text file, you need to go in serial fashion

File operations on binary file

fopen – File open

The library function to open a binary file is same as for text file, however the mode identifies the binary file operations. Syntax for fopen is:

FILE * fopen(const char *filename, const char *mode);

First argument to the function is the filename on disk that we want to open. The filename can include the complete path of the file to be opened. If complete path is not specified, the function looks for the file in current directory.

Second argument to the function is mode. When we open a file we need to specify the kind of operation we want to perform on the file. E.g. we want to read data, write data, read and write both and so on. For binary file operations, following are the possible values of mode:

rb – open the file for reading in binary mode

wb – open the file for writing in binary mode. Create the file if it doesn’t exist and truncate the contents of file if it exists.

r+b – open the file for read and write operations in binary mode.

w+b – open the file for read and write operations in binary mode. Create the file if it doesn’t exist and truncate the contents of file if it exists.

ab – open the file in append mode for write operations in binary mode. Create the file if it doesn’t exist.

a+b – open the file for read operations and append mode for write operations in binary mode. Create the file if it doesn’t exist.

fwrite – File write

The library function used to write contents to a binary file is fwrite. Syntax for the library call is :

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

The function fwrite takes four arguments

  • ptr gives the location from where fwrite takes data to write
  • size is the total number of bytes to be written
  • nmemb is the number of items to be written
  • stream is the file pointer of file to write the data in binary format

On success it returns number of items written. If an error occurs, the return value is a short item count (or zero).

fread – File read

The library function used to read contents from a binary file is fread. Syntax for the library call is:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE * stream );

The function fread takes four arguments

  •  ptr gives the location where fread will store the data that it reads
  • size is the total number of bytes to be read
  • nmemb is the number of items to be read
  • stream is the file pointer of file from where to read the data in binary format

On success it returns number of items read. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

fseek – File seek

The fseek function moves the file pointer to a given location in the file. Syntax for the library call is

int fseek(FILE *stream, long offset, int whence);

where

  • stream is the pointer to file for which fseek function shall set the file-position indicator
  • offset is added to the position specified by whence to obtain the location that fseek shall set the file position to.
  • whence specifies the reference point from where file seek operation is to be performed
  • SEEK_SET – reference point is beginning of the file
  • SEEK_CUR – reference point is current location in the file
  • SEEK_END – reference point is end of the file. offset in this case should be a negative value