Store records in a file using structure - C

Write a C program to store record of student (stud_no, stud_name, stud_addr, stud_Percentage) in a file using structure.

Solution:

#include<stdio.h>
struct stud
{
      int rno;
      float per;
      char name[20],add[20];
}s;
int main()
{
      FILE *fp;
      fp=fopen("student.txt","w");
      printf("Enter record of student:\n\n");
      printf("\nEnter student number : ");
      scanf("%d",&s.rno);
      printf("\nEnter name of student: ");
      scanf("%s",s.name);
      printf("\nEnter student address : ");
      scanf("%s",s.add);
      printf("\nEnter percentage of student : ");
      scanf("%f",&s.per);
      fprintf(fp,"%d\n%s\n%s\n%f",s.rno,s.name,s.add,s.per);
      printf("\nRecord stored in file...");
      fclose(fp);
      return 0;
}


Output:

student record

Following is the stud.txt file which stores entered record.

student file