fwrite() in C Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In C, fwrite() is a built-in function used to write a block of data from the program into a file. It can write arrays, structs, or other data to files but is especially designed to write binary data in binary files.Example: C #include <stdio.h> int main() { FILE *fptr = fopen("gfg.bin", "wb"); int a[] = {1, 2, 3, 4, 5}; int n = sizeof(a) / sizeof(a[0]); // Write array a[] into the file using fwrite fwrite(a, sizeof(int), n, fptr); fclose(fptr); return 0; } The gfg.bin will contain the following data (in binary form):12345Explanation: In the given program, fwrite() writes the integer array a[] as raw binary data to the file gfg.bin. It writes n elements of size sizeof(int) from the array a to the file. This article covers the syntax, uses and common examples of fwrite() functions in C.Table of ContentSyntax of fwrite()Examples of fwrite()Writing a String to a Text FileAppending Data Using fwrite()Writing File MetaData Syntax of fwrite()fwrite() is a standard library function defined in <stdio.h> header file in C.fwrite(a, size, count, fptr);Parameters:a: Name of the array to be written.size: Size of each element.count: Number of elements to be written.fptr: Pointer to the file.Return Value:It returns the number of objects written successfully.This return value is generally used to check whether the write operation was successful or not.Examples of fwrite()The following examples demonstrate use of fwrite() function in C programs.Writing a String to a Text File C #include <stdio.h> #include <string.h> int main() { FILE *fptr = fopen("gfg.txt", "w"); // Create a string for write into the file char s[] = "Hello, geeksforgeeks!"; // Wrtie the string into the file using fwrite int n = fwrite(s, sizeof(char), strlen(s), fptr); // Here we check whole file is written into file if(strlen(s) == n){ printf("String written successfully"); } fclose(fptr); return 0; } OutputString written successfullyWriting a Structure to a FileA structure to a file can be written by fwrite() function as the raw binary data. C #include <stdio.h> #include <string.h> // Create a struct for inserting typedef struct { int a; int b; char s[20]; }GfG; int main() { FILE *fptr = fopen("gfg.bin", "wb"); GfG gfg = {1, 999, "GeeksforGeeks"}; // Wrtie the gfgHeader data into the file using fwrite. int n = fwrite(&gfg, sizeof(gfg), 1, fptr); // Check data written successfully. if(n == 1){ printf("Structure written successfully"); } fclose(fptr); return 0; } OutputStructure written successfully Comment More infoAdvertise with us Next Article C Arrays R ritikk5g6k Follow Improve Article Tags : C Language C-Functions C-File Handling Similar Reads C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin 6 min read Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where 5 min read Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this, 9 min read C Arrays An array in C is a fixed-size collection of similar data items.Items are stored in contiguous memory locations. Can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc.C// A simple C 7 min read C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and 8 min read C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor 9 min read Operators in C Operators are the basic components of C programming. They are symbols that represent some kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which are to be performed on values or variables. The values and variables used with operators are called oper 11 min read C Programming Interview Questions (2025) At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the oldest and most popular programming languages. There are many applications in which C programming language is us 15+ min read Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are 6 min read Like