How to Read a Struct from a Binary File in C? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The structure in C allows the users to create user-defined data types that can be used to group data items of different types into a single unit. We can save this structure in a file using C file handling. In this article, we will learn how we can read a struct from a binary file in C. Read a Struct from a Binary File in CTo read a struct from a binary file in C, we will first have to open the binary file using the fopen() method in rb mode. Then we will use the fread() function to read the structure from the file. Syntax of fread()size_t fread(void * buffer, size_t size, size_t count, FILE * stream);here, buffer: It refers to the pointer to the buffer memory block where the data read will be stored.size: It refers to the size of each element in bytes.count: It refers to the count of elements to be read.stream: It refers to the pointer to the file stream.C Program to Read a Struct from a Binary File C // C program to read a struct from a binary file #include <stdio.h> typedef struct { int id; char name[50]; float salary; } Employee; int main() { // open the file in rb mode FILE* file = fopen("employee_data.bin", "rb"); // check if the file was successfully opened if (file == NULL) { perror("Error opening file"); return 1; } // Define the struct Employee employee; // Read the structs present in the file while (fread(&employee, sizeof(Employee), 1, file) == 1) { // Process the read data (e.g., print or manipulate) printf("Employee ID: %d, Name: %s, Salary: %.2f\n", employee.id, employee.name, employee.salary); } // close the file fclose(file); return 0; } Output Employee ID: 1, Name: John Doe, Salary: 50000.0Employee ID: 2, Name: Jane Smith, Salary: 60000.0Time Complexity: O(N) where N is the number of structs in the fileAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Read a Struct from a Binary File in C? T thewebdevjwnb Follow Improve Article Tags : C Programs C Language C-Structure & Union C-File Handling C Examples +1 More Similar Reads How to Write a Struct to a Binary File in C? C file handling allows users to store the data from a C program to a file in either the text or the binary format. In this article, we will learn how to write a struct to a binary file in C. Writing Structure into a Binary File in CTo write a struct to a binary file, we can use the fwrite() function 2 min read How to Read From a File in C? File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. In this article, 2 min read How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read How to Read a File Line by Line in C? In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file.Reading a File Line by Line in CReading a file line by line is a step by step:1. Opening th 3 min read How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read How to Create a Dynamic Array Inside a Structure? In C, the structure can store the array data types as one of its members. In this article, we will learn how to create a dynamic array inside a structure in C. Creating a Dynamic Array Inside a Structure in CTo create a dynamic array inside a structure in C, define a structure that contains a pointe 2 min read How to Pack a Struct in C? In C, when you declare a structure, the compiler allocates memory for its members, and the way it does this can involve adding padding between members for alignment purposes. struct packing refers to the arrangement of the members of a structure in memory so that there is no extra space left. In thi 1 min read How to Declare a Pointer to a Struct in C? Structure (or structs) in the C programming language provides a way to combine variables of several data types under one name and pointers provide a means of storing memory addresses. In this article, we will learn how to declare such a pointer to a struct in C. Declaration of Pointer to Struct in C 2 min read How to Access Array of Structure in C? In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t 2 min read Like