C Program to Implement Student Records using Files

This is the C Program implementation of student records (Structure) where the records are stored in files,the records can be inserted,modified,deleted or displayed by the user

[cc lang=”c”]
#include
#include

int JP=0;
FILE *jp;

typedef struct record
{
char name[50];
int no,mark1,mark2,mark3,total;
double avg;
}student;

student std[100];

int nRec;

main()
{
menu();
}

menu()
{
int ch;
printf(“nChoose any Option n1.INSERTn2.DELETEn3.SEARCHn4.SORTn5.UPDATEn6.EXITn7.Previewn”);
scanf(“%d”,&ch);
do{
switch(ch)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: search(); break;
case 4: sort(); break;
case 5: update(); break;
case 7:preview(); break;
default:printf(“Invliad Optionn”);
}
}
while(ch!=6);
}

preview()
{
student std;
jp=fopen(“data.jp”,”r”);
while(!feof(jp))
{
fread(&std,sizeof(student),1,jp);
if(!feof(jp))
printf(“%d. %st%dt%lf n”,std.no,std.name,std.total,std.avg);
}
fclose(jp);
}

search()
{
char src[100];
printf(“Enter the Name of The Student to Searchn”);
scanf(“%s”,src);
int pos=0;
student std;
jp=fopen(“data.jp”,”r”);
while(!feof(jp))
{
fread(&std,sizeof(student),1,jp);
if(strcmp(std.name,src)==0)
{
fclose(jp);
pos++;
printf(“Found at Position %dn”,pos);
}
pos++;
}
menu();
}

writefile()
{
int i;
jp=fopen(“data.jp”,”w”);
for(i=0;istd[j].avg)
{
temp=std[i];
std[i]=std[j];
std[j]=temp;
}
}
}
writefile();
menu();
}

swap(int i)
{
student temp;
int j=i;
if(i==nRec-1)
{
i–;
}
else
for(;j
#include
typedef struct
{
int month,day,year;
}date;

typedef struct
{
char name[80],street[80],city[80],acct_type;
int acct_no;
float oldbalance,newbalance,payment;
date lastpayment;
}record;

FILE *ptold,*ptnew;
int month,day,year;

main()
{
record customer;
ptold=fopen(“records.old”,”r”);
ptnew=fopen(“records.new”,”w”);
printf(“CUSTOMER BILLING SYSTEM UPDATE nEnter date”);
scanf(“%d%d%d”,&day,&month,&year);
while(1)
{
fscanf(ptold,” %[^n]”,customer.name);
fprintf(ptnew,”n%sn”,customer.name);
if(strcmp(customer.name, “END”)==0)
break;
customer=readfile(customer);
customer=update(customer);
writefile(customer);
}
fclose(ptold);fclose(ptnew);
}

record readfile(record customer)
{
fscanf(ptold,” %[^n]”,customer.street);
fscanf(ptold,” %[^n]”,customer.city);
fscanf(ptold,”%d”,&customer.acct_no);
fscanf(ptold,”%c”,&customer.acct_type);
fscanf(ptold,”%f”,&customer.oldbalance);
fscanf(ptold,”%f”,&customer.newbalance);
fscanf(ptold,”%f”,&customer.payment);
fscanf(ptold,”%d%d%d”,&customer.lastpayment.day,&customer.lastpayment.month,&customer.lastpayment.year);
return(customer);
}

record update(record customer)
{
printf(“n Name: %s”,customer.name);
printf(“nAccount Number: %dn”,customer.acct_no);
printf(“nOldbalance %fn”,customer.oldbalance);
printf(“Current Paymentn”);
scanf(“%f”,&customer.payment);

if(customer.payment>0)
{
customer.lastpayment.month=month;
customer.lastpayment.day=day;
customer.lastpayment.year=year;
customer.acct_type=(customer.payment<0.1*customer.oldbalance)?'D':'C'; customer.newbalance=customer.oldbalance-customer.payment; printf("CNew BAlance %fn",customer.newbalance); switch(customer.acct_type) { case 'C':printf("CURRENTn"); break; case 'O':printf("OVERDUEn"); break; case 'D':printf("DELINQUENTn"); break; default:printf("ERRORn"); } return(customer); } void writefile(record customer) { fprintf(ptnew," %sn",customer.street); fprintf(ptnew," %sn",customer.city); fprintf(ptnew," %dn",customer.acct_no); fprintf(ptnew," %cn",customer.acct_type); fprintf(ptnew," %.2fn",customer.oldbalance); fprintf(ptnew," %.2fn",customer.newbalance); fprintf(ptnew," %.2fn",customer.payment); fprintf(ptnew,"%d%d%dn",customer.lastpayment.day ,customer.lastpayment.month ,customer.lastpayment.year); printf("n Name: %s",customer.name); } [/cc]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *