Writing & Reading Binary Files in C Programming

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Practical Application for C Programming: Working with Files

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Reading & Writing from…
  • 3:22 Examples Using fread()…
  • 9:20 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up


Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami
This lesson demonstrates how to perform read and write operations on a binary file in C programming language. We'll also discuss some of the benefits of working with binary files.

Reading and Writing from Binary Files

Binary files are very similar to arrays except for the fact that arrays are temporary storage in the memory but binary files are permanent storage in the disks. The most important difference between binary files and a text file is that in a binary file, you can seek, write, or read from any position inside the file and insert structures directly into the files. You must be wondering - why do we need binary files when we already know how to handle plaintexts and text files? Here are the reasons why binary files are necessary:

1. I/O operations are much faster with binary data.

Usually, large text files contain millions of numbers. It takes a lot of time to convert 32-bit integers to readable characters. This conversion is not required in the case of binary files as data can be directly stored in the form of bits.

2. Binary files are much smaller in size than text files.

For data that is in the form of images, audio or video, this is very important. Small size means less storage space and faster transmission. For example, a storage device can store a large amount of binary data as compared to data in character format.

3. Some data cannot be converted to character formats.

For example, Java compilers generate bytecodes after compilation.

Having said that, let's move on to handling I/O operations in a binary file in C. The basic parameters that the read and write functions of binary files accept are:

  • the memory address of the value to be written or read
  • the number of bytes to read per block
  • the total number of blocks to read
  • the file pointer

There are functions provided by C libraries to seek, read, and write to binary files. Let's explain this by reading and writing a structure called rec in a binary file. The structure is defined like this:

/* The structure to be inserted in the binary file */
struct record

 int a,b,c;

The fread() function is used to read a specific number of bytes from the file. An example of fread() looks like this:

fread(&myRecord, sizeof(struct record), 1, ptr);

This statement reads 'a' bytes (in this case, it's the size of the structure) from the file into the memory address &myRecord. Here the number 1 denotes the number of blocks of 'a' bytes to be read. If we change it to 10, then it denotes 10 blocks of 'a' bytes will be read and stored into &myRecord. ptr is the pointer to the location of the file that is being read.

Now the fwrite() function is used to write to a binary file, like so:

fwrite(&myRecord, sizeof(struct record), 1, ptr);

In this example, the value inside the address &myRecord which is of the size of the structure record is written into the file with the help of the file pointer ptr.

Examples using fread() & frwrite()

Now that you know how to read and write binary files, let's discuss this with the help of examples, starting with an example of using fread().

/* Our structure */
struct record

  int a,b,c;
int main()

 int count;
 FILE *ptr;
 struct record myRecord;
 if (!ptr)
    printf("Unable to open file!");     return 1;
 for ( count=1; count <= 10; count++)
  fread(&myRecord,sizeof(struct record),1,ptr);   printf("%d\n",myRecord.a);
 }  fclose(ptr);
 return 0;

In this example, we tried to read each structure from the binary file using the fread() function. This reads the structure one byte at a time and stores it inside the address myRecord. Let's now look at an example using fwrite().

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account