70 likes | 181 Views
Program in C to read 'mark.dat' file with student info, calculate total, grade, and store in 'result.dat'. Make use of fread and fwrite functions.
E N D
Write a C language program to read “mark.dat” file containing rollno, name,marks of three subjects and calculate total mark, result in grade and store same in “result.dat” file. (Note : Make use of fread and fwrite functions) #include<stdio.h> #include<conio.h> main() { struct stud { char name[20]; int rollno; int marks[3]; }; typedef struct stud student;
FILE *fp,*result; int i,j,n,total=0; student *s; //File fp and result opened in read-write mode fp=fopen("mark.dat","w+"); result=fopen("result.dat","w+"); clrscr(); printf("Enter the no. of students :"); scanf("%d",&n); for(i=0;i<n;i++) { printf(" student Name :"); scanf("%s",s->name); printf("Roll No:"); scanf("%d",&s->rollno); printf("Marks in three subjects:"); for(j=0;j<3;j++) scanf("%d",&s->marks[j]); fwrite(s,sizeof(student),1,fp);//one student record is written onto the file fp }
rewind(fp); for(i=0;i<n;i++) { fread(s,sizeof(student),1,fp); total=0; for(j=0;j<3;j++) { printf(" \nMarks[%d]: %d",j+1,s->marks[j]); total=total+s->marks[j]; } fwrite(s,sizeof(student),1,result); fprintf(result,"%d ",total); }
rewind(result); for(i=0;i<n;i++) { fread(s,sizeof(student),1,result); printf(" \nstudent Name %s",s->name); printf("\nRoll No:%d",s->rollno); printf("\nMarks in three subjects:"); for(j=0;j<3;j++) { printf(" \nMarks[%d]: %d",j+1,s->marks[j]); } fscanf(result,"%d ",&total); printf("Total =%d",total); } fclose(fp); fclose(result); getch(); }