Write a C program to create file for N students, it should contain Roll-No, Name and Marks in two subjects. Using the above created file, create an out put file which contains Roll-No, Name, Marks in subjects, Total and Average.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 FILE *fp,*fout;
 int rollno,n,sub1,sub2,total,i;
 char name[20];
 float avg;
 clrscr();
 fp=fopen("student.dad","w");
 if(fp==NULL)
 {
 puts("\n file cannot be opened");
 getch();
 exit(0);
 }
 printf("\n enter the number of students:");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  printf("student %d",i+1);
  printf("enter the rollno,name,marks in sub1,marks in sub2\n");
  scanf("%d%s%d%d",&rollno,name,&sub1,&sub2);
  fprintf(fp,"%d%s%d%d",rollno,name,sub1,sub2);
  }
  fclose(fp);
  printf("\n student file created successfully ");
  fp=fopen("student.dat","r");
  if(fp==NULL)
  {
   puts("\n file cannot be opened");
   getch();
   exit(0);
  }
  fout=fopen("output.dat","w");
  if(fout==NULL)
  {
  puts("\n file cannot be opened ");
  getch();
  exit(0);
  }
 fprintf(fout,"\n rollno name sub1 sub 2 total average \n\n");
 while(fscanf(fp,"%d%s%d%d",&rollno,name,&sub1,&sub2)!=EOF)
 {
  total=sub1+sub2;
  avg=total/2.0;
  fprintf(fout,"%d\t%s\t%d\t%d\n",rollno,name,sub1,sub2,total,avg);
  }
  fclose(fout);
  }

3 comments:

  1. thnx i was searching for this

    ReplyDelete
  2. thnx i was searching for this

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete