Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The fwrite() function

The function fwrite() is used to write data into the binary file.

Syntax

size_type fwrite(const void *p, size_type size, size_type number, File *fp);

That is

size_of_item fwrite(address_of_item, size_of_item, number_of_items, pointer_to_file);

*p – Address of data/item that is to be written to the file.
size – Size of data/item that is to be written to the file.
number – Number of items to be written to the file.
*fp – Points to the file to which data is to be written.

Example

#include<stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

struct Employee emp = {19,"VICKY",75.67};

int main()
{
    FILE *fp;
    fp = fopen("D:\\C Programming\\file7","wb");
	
    if(fp==NULL)
    {
        printf("\n\n Unable to open the file");
        return 1;
    }
	
    fwrite(&emp,sizeof(struct Employee),1,fp);
    fclose(fp);
	
    return 0;
}

 

Leave a reply