C Language



Admission Enquiry Form

  

Write and Read a Structure(fwrite() and fread() functions)

fwrite() function

The fwrite() function is used to write records or sequence of bytes or we can say binary data to the file. A record which we are writing may be an array or a structure.

Syntax of fwrite() function

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.




Program of fwrite() function to write a structure:

//program to use fwrite in file handling in c
#include<stdio.h>
struct student
{
char name[15];
int rollno;
float fee;

};//end of struct student
void main()
{
struct student st;
FILE *fptr;
printf("Enter name of student:");
scanf("%s",st.name);
printf("Enter rollno of student:");
scanf("%d",&st.rollno);
printf("Enter fee of student:");
scanf("%f",&st.fee);

fptr=fopen("fwrite.txt","a");
if(fptr!=NULL)
{
fwrite(&st,sizeof(st),1,fptr);
printf("\n Data saved into the file");

}
else
{
printf("\nCould not open file");

}
fclose(fptr);//closing file
}//end of main


Output:

Enter name of student:neha
Enter rollno of student:177
Enter fee of student:45000

Data saved into the file




fread() function

The fread() function is used to read bytes or binary data from the file.

Syntax of fread() function

fread( ptr, int size, int n, FILE *fp );

The fread() function also takes four arguments.
ptr : ptr is the reference of an array or a structure where data will be stored after reading.
size : size is the total number of bytes to be read from file.
n : n is number of times a record will be read.
FILE* : FILE* is a file where the records will be read.




Program of fread() function to read a structure:

//program to use fread in file handling in c
#include<stdio.h>
struct student
{
char name[15];
int rollno;
float fee;
};//end of struct student
void main()
{
struct student st;
FILE *fptr;

fptr=fopen("fwrite.txt","r");
if(fptr!=NULL)
{
fread(&st,sizeof(st),1,fptr);
printf("\nName of student is : %s",st.name);
printf("\n Rollno of student is :%d",st.rollno);
printf("\n Fee of student is :%f",st.fee);
}
else
{
printf("\nCould not open file");

}
fclose(fptr);//closing file
}//end of main


Output:

Name of student is : neha
Rollno of student is :177
Fee of student is :45000.000000




Program of fread() function to read multiple records from a file of structure:

//program to use fread to read multiple records from a file in c file handling
#include<stdio.h>
struct student
{
char name[15];
int rollno;
float fee;

};//end of struct student
void main()
{
struct student st;
FILE *fptr;
fptr=fopen("fwrite.txt","r");
if(fptr!=NULL)
{
while(fread(&st,sizeof(st),1,fptr))
{
printf("\n Name is :%s",st.name);
printf("\n Rollno is :%d",st.rollno);
printf("\nFee is :%f",st.fee);
printf("\n********************************************");
}
}
else
{
printf("\nCould not open file");
}
fclose(fptr);//closing file
}//end of main


Output:

Name of student is : neha
Rollno of student is :177
Fee of student is :45000.000000
********************************************
Name of student is : priya
Rollno of student is :178
Fee of student is :55000.000000
********************************************
Name of student is : ritu
Rollno of student is :178
Fee of student is :65000.000000
********************************************