C Program to Input Student Details into a File For Two Subjects

How to write a C program to input student details into a file for two subjects in C Programming Language ?


Solution:
/*Entry of student's details into a file for two subjects */




#include <stdio.h>

#define HIGH_MARK 100
#define SIZE 2

int main()
{
FILE *fp;
char fname[10];
int i,total,n;
float avg,percent;

struct student
{
char name[10];
int rollno;
char subject1[10];
int s1marks,s2marks;
char subject2[10];
};
struct student s[10];


printf("\nEnter the name of the file\n");
scanf("%s",fname);

printf("Enter the number of student's details you want to enter\n");
scanf("%d",&n);

fp=fopen(fname, "wa+");

if(fp == NULL)
{
printf("\nThe file open attempt was unsuccessful\n");
}

for(i=0;i<n;i++)
{
printf("\n\t\tSTUDENT RECORD\t\t\nEnter\n\nName:\t");
scanf("%s",s[i].name);

printf("\nRollno:\t");
scanf("%d",&s[i].rollno);

printf("\nSubject-1:");
scanf("%s",s[i].subject1);

printf("\nMarks:\t");
scanf("%d",&s[i].s1marks);

printf("\nSubject-2:");
scanf("%s",s[i].subject2);

printf("\nMarks:\t");
scanf("%d",&s[i].s2marks);


fprintf(fp,"\n\t\tSTUDENT RECORD\t\t\nENTER\n\nNAME:\t\t");
fprintf(fp,"%s",s[i].name);

fprintf(fp,"\nROLL NO:\t");
fprintf(fp,"%d",s[i].rollno);

fprintf(fp,"\nSUBJECT-1:\t");
fprintf(fp,"%s",s[i].subject1);


fprintf(fp,"\nMARKS:\t\t");
fprintf(fp,"%d",s[i].s1marks);

fprintf(fp,"\nSUBJECT-2:\t");
fprintf(fp,"%s",s[i].subject2);


fprintf(fp,"\nMARKS:\t\t");
fprintf(fp,"%d\n",s[i].s2marks);

total=((s[i].s1marks)+(s[i].s2marks));
avg=total/SIZE;
percent=((((float) total)/(SIZE*HIGH_MARK))*100);

fprintf(fp,"\nThe total of all the marks the student obtained is:\t");
fprintf(fp,"%d",total);

fprintf(fp,"\nThe average of the marks obtained is:\t");
fprintf(fp,"%f\n",avg);

fprintf(fp,"\nThe percentage obtained is ");
fprintf(fp,"%f% \n",percent);

if(percent > 80)
{
fprintf(fp,"\nThe student passed in distinction\n\n");
}
else if(percent <50)
{
fprintf(fp,"\nThe student is Failed\n");
}
}
}



Learn More :