fwrite() Function in C Programming

aimtocode

The fwrite() function

The fwrite() function is used to write records (sequence of bytes) to the file. A record may be an array or a structure.

Syntax

  fwrite( ptr, int size, int n, FILE *fp );
  • The fwrite() function takes four arguments.
  • ptr : ptr is the reference of an array or a structure stored in memory.
  • size : size is the total number of bytes to be written.
  • n : n is number of times a record will be written.
  • FILE* : FILE* is a file where the records will be written in binary mode.

Example:



Output:

 Enter Roll : 1
 Enter Name : Ashish
 Enter Marks : 78.53
 Do you want to add another data (y/n) : y
 Enter Roll : 2
 Enter Name : Kaushal
 Enter Marks : 72.65
 Do you want to add another data (y/n) : y
 Enter Roll : 3
 Enter Name : Vishwas
 Enter Marks : 82.65
 Do you want to add another data (y/n) : n
 Data written successfully...


aimtocode