fwrite() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The function fwrite() is a standard library function in C language, present in the stdio.h header file, which allows us to write data into a file, generally, the file here is a binary file, but we can use it with text files as well.

Syntax of fwrite() Function in C

Parameters of fwrite() Function in C

The function fwrite in C receives 4 parameters

  • ptr : pointer to the block of memory or the array from where the elements are to be copied
  • size : size of each element in bytes which are to be written
  • count : specifies the number of elements that are to be written
  • stream : pointer to a FILE object, where the data read will be written

Return Value of fwrite() Function in C

The function fwrite in C returns an int value, more precisely a size_t value, which in turn is an integral data type, Here the function returns the number of elements, which are successfully written to the FILE object, it is generally equal to the value of the count parameter, but there can be possible cases, where some error has occurred while writing the data into the file, and then, in that case, the value returned can be less than the value of count as well.

If in the function call, the value of size or count are passed as 0, then the function fwrite() returns 0 and performs no other proceeding action

Example

In the below example, we will be writing a character array into a file using the function fwrite in C.

After successful execution of the above code

The file will contain the following data

Remember - If the specified file does not exist, then the function will create the file on its own and will write the passed data into it, but if the file exists, then all of its content will be removed first, and then the passed data will be overwritten into it.

Exceptions of fwrite() Function in C

There are as such no particular exceptions to the function fwrite in C.

What is fwrite() Function in C?

The function fwrite() is a standard library function in C language that is used to write a block of data from the memory into a file. It takes four compulsory parameters - a pointer to the block of memory from where the data is to be copied, the size of each data element in bytes, a count of the number of elements to write, and a pointer to the FILE object where the read data will be written. The function writes into the file up to the count number of elements, from the pointer or memory location specified to the provided file stream.

fwrite() is generally used to write binary data into a file, but we can use it with text mode as well.

Note - As in binary mode, the data in a file will be stored in the same way as it will be stored in the actual memory, and hence no transformation of data will be taking place ie. there will be no additional process for conversion of the text data into the binary data, which in turns makes the binary mode of writing data slightly faster than the text mode.

More Examples

1. Writing a Variable into a File Using fwrite() Function in C

In the below example, we will be writing a variable into a file using the fwrite() function

After successful execution of the above code

The file will contain the following data

The reason for the above result ie. the written data is the alphabet A instead of the integer 65 is the ASCII value of the variable num, as the function fwrite() operates binarily, hence the integer data 65 is implicitly mapped with its respective ASCII value which is the alphabet A and therefore we get the above result.

2. Writing an Array into a File Using the fwrite() Function in C

In the below example, we will be writing an integer array into a file using the fwrite() function

After successful execution of the above code

The file will contain the following data

3. Writing a Structure into a File Using fwrite() Function in C

In the below example, we will be writing a structure variable into a file using the fwrite() function.

Output

After successful execution of the above code

The file will contain the following data

4. Writing an Array of Structures into a File Using the fwrite() Function in C

In the below example, we will be writing an array of structures into a file using the function fwrite in c

After successful execution of the above code

The file will contain the following data

Conclusion

  • fwrite() is a standard library function in C language, which is used to write data into binary files, but can be used with text files as well.
  • Syntax : size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
  • It takes four parameters - a pointer to the block of memory from where the elements are to be copied, size of each element in bytes that are to be written, the number of elements to write, and a pointer to the FILE object.
  • The function fwrite() returns an integer value which denotes the number of elements that are successfully written into the file, it is always less than or equal to the count parameter.