1 / 5

File handling in C – A review

File handling in C – A review. 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>

sandlin
Download Presentation

File handling in C – A review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File handling in C – A review

  2. 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;

  3. 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 }

  4. 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); }

  5. 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(); }

More Related