Friday, June 26, 2009

PROGRAM TO INPUT RECORDS FOR A FILE AND CALCULATE AVERAGE MARKS BY REOPENING THE FILE

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

struct students
{
char name[30];
int roll_no;
int marks;
};

void main()
{
struct students stu;
FILE *record;
int i,N,total=0;
float average;
clrscr();
printf("How many students are there? ");
scanf("%d",&N);

record=fopen("data.txt","wb");
for(i=0;i {
fflush(stdin);
printf("\nName: ");
scanf("%[^\n]",stu.name);
fflush(stdin);
printf("Roll No: ");
scanf("%d",&stu.roll_no);
printf("Marks: ");
scanf("%d",&stu.marks);
fflush(stdin);
fwrite(&stu,sizeof(struct students),1,record);
}
fclose(record);

record=fopen("data.txt","rb");
if(record==NULL)
{
printf("couldn't open the file...");
exit(0);
}

for(i=0;i {
fread(&stu,sizeof(struct students),1,record);
total+=stu.marks;
}

average=(float)total/N;
printf("\nAverage - %.2f",average);
fclose(record);
getch();
}

0 comments:


by Ankit Pokhrel.