Write a c program to store the records of student (stud_no, stud_name, stud_addr, stud_percentage) in a file using structure.

#include<stdio.h>
void main()
{
FILE *fp;
char *fname,ch='y';
int n=0;
struct stud
{
int stud_no;
char stud_name[20];
char stud_addr[20];
float stud_percentage;
}s;
clrscr();
printf("\n enter source file name:\t");
gets(fname);
fp=fopen(fname,"w");
if(fp==NULL)
{
printf("\n unable to open");
exit(0);
}
while(ch=='Y'||ch=='y')
{
printf("\n enter student number");
scanf("%d",&s.stud_no);
printf("\n enter student name:\t");
scanf("%s",s.stud_name);
printf("\n enter stud address\n");
scanf("%s",s.stud_addr);
printf("\n enter percentage");
scanf("%f",&s.stud_percentage);
fprintf(fp,"%d%s%s%f",s.stud_no,s.stud_name,s.stud_addr,s.stud_percentage);
n++;
printf("\n do you want to add another record (Y/N)?");
fflush(stdin);
scanf("%c",&ch);
}
printf("\n%d record are store in %s file",n,fname);
fclose(fp);
getch();
}

2 comments: