How to Write a Struct to a Binary File in C? Last Updated : 27 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 that writes the given bytes of data in the file in the binary form. Syntax of fwrite()size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)where, ptr: pointer to the block of memory to be written.size: the size of each element to be written (in bytes).nmemb: umber of elements.stream: FILE pointer to the output file stream.ApproachOpen the binary file in write modeCheck if the file is opened successfully. If not print error and exit.Specify the pointer to the struct, the size of the struct, and the number of instances.Write the data of the struct to the file using the frwite() method.Check if the write operation was successful. If not return an error.Close the file.C Program to Write a Struct to a Binary FileThe following program illustrates how we can write a struct to a binary file in C: C // C Program to illustrates how we can write a struct to a // binary file #include <stdio.h> // Define a struct struct Person { char name[50]; int age; float height; }; int main() { // Create an instance of the struct struct Person person = { "John", 35, 6.0 }; // Open a file in binary write mode FILE* file = fopen("person_data.bin", "wb"); if (file == NULL) { perror("Error opening file"); return 1; } // Write the struct data to the file size_t num_written = fwrite(&person, sizeof(struct Person), 1, file); if (num_written != 1) { perror("Error writing to file"); fclose(file); return 1; } // Close the file fclose(file); printf("Struct data written to Binary file " "successfully.\n"); return 0; } OutputStruct data written to Binary file successfully. Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Write a Struct to a Binary File in C? R rohitpatel0001 Follow Improve Article Tags : C Programs C Language C-Structure & Union C-File Handling C Examples +1 More Similar Reads How to Read a Struct from a Binary File in C? 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 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 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 use typedef for a Struct in C? In C, we use typedef to create aliases for already existing types. For structure, we can define a new name that can be used in place of the original struct name. In this article, we will learn how to create a typedef for a structure in C++. Use the typedef struct in CTo create an alias for a structu 2 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 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 write in a file using fputs() in C fputs() is a function declared in stdio.h header file. It is used to write the contents of the file. The function takes 2 arguments. The first argument is a pointer to the string which is to be written and the second argument is the pointer of the file where the string is to be written. It returns 1 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 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 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 Like