Program to create a data file to store student information Such as Roll. No., Name, Class and Total Marks. Copy those Records to where total marks is more than 400 to another File. Display the contents of both.
#include<stdio.h>
main()
{
FILE
*f1,*f2;
char
ans,name[25],class[8];
int
rno,i,tot;
clrscr();
f1=fopen("STUD.DAT","w");
do
{
printf("\nEnter
the Roll No.:\n");
scanf("%d",&rno);
printf("\nEnter
the name\n");
scanf("%s",name);
printf("\nEnter
the class\n");
scanf("%s",class);
printf("\nEnter
the Marks:\n");
scanf("%d",&tot);
fprintf(f1,"\n%d%s%s%d",rno,name,class,tot);
fflush(stdin);
printf("\nContinue
Input?");
scanf("%c",&ans);
}while(ans=='Y'||ans=='y');
fclose(f1);
f1=fopen("STUD.DAT","r");
f2=fopen("MARKS.DAT","w");
while(fscanf(f1,"%d%s%s%d",&rno,name,class,&tot)!=EOF)
{
if(tot>400)
{
fprintf(f2,"\n%d%s%s%d",rno,name,class,tot);
}
}
fclose(f1);
fclose(f2);
printf("\nThe
contents of the files are:");
f1=fopen("STUD.DAT","r");
f2=fopen("MARKS.DAT","r");
while((fscanf(f1,"%d%s%s%d",&rno,name,class,&tot))!=EOF)
printf("\nRoll
NO.: %d, Name: %s Class: %s Marks: %d",rno,name,class,tot);
printf("\nContents
of the File 2 are:");
while((fscanf(f2,"%d%s%s%d",&rno,name,class,&tot))!=EOF)
{
printf("\nR.No:
%d Name: %s Class:%s
Total=%d",rno,name,class,tot);
}
fclose(f1);
fclose(f2);
getch();
}
Comments
Post a Comment