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
#include<string.h>
#include<stdio.h>
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;i<nRec;i++)
{
fwrite(&std[i],sizeof(student),1,jp);
}
fclose(jp);
}
update()
{
char upd[40];
int u,i;
printf("Enter The Record to Updaten");
scanf("%s",upd);
for(i=0;i<nRec;i++)
{
if(strcmp(std[i].name,upd)==0)
{
u=i;
}
}
printf("nEnter Name,Roll NO,Mark1,Mark2,Mark3n");
scanf("%s",std[u].name);
scanf("%d",&std[u].no);
scanf("%d",&std[u].mark1);
scanf("%d",&std[u].mark2);
scanf("%d",&std[u].mark3);
std[u].total=std[u].mark1+std[u].mark2+std[u].mark3;
std[u].avg=std[u].total/3;
writefile();
menu();
}
sort()
{
int i=0,j;
student temp;
for(;i<nRec;i++)
{
for(j=i+1;j<nRec;j++)
{
if(std[i].avg>std[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<nRec;j++)
{
temp=std[j];
std[j]=std[j+1];
std[j+1]=temp;
}
}
delete()
{
char del[40];
int i;
printf("Enter the Name of The Student to Deleten");
scanf("%s",del);
for(i=0;i<nRec;i++)
{
if(strcmp(std[i].name,del)==0)
{
swap(i);
}
}
writefile();
}
insert()
{
int j,i;
//if(JP==0)
//{
printf("How Many Records to Insertn");
scanf("%d",&nRec);
for(i=0;i<nRec;i++)
{
printf("nEnter Name,Roll NO,Mark1,Mark2,Mark3n");
scanf("%s",std[i].name);
scanf("%d",&std[i].no);
scanf("%d",&std[i].mark1);
scanf("%d",&std[i].mark2);
scanf("%d",&std[i].mark3);
std[i].total=std[i].mark1+std[i].mark2+std[i].mark3;
std[i].avg=std[i].total/3;
JP++;
writefile();
}
menu();
}
//else
//{
/*j=nRec+1;
printf("nEnter Name,Roll NO,Mark1,Mark2,Mark3n");
scanf("%s",std[j].name);
scanf("%d",&std[j].no);
scanf("%d",&std[j].mark1);
scanf("%d",&std[j].mark2);
scanf("%d",&std[j].mark3);
std[j].total=std[j].mark1+std[j].mark2+std[j].mark3;
std[j].avg=std[j].total/3;
writefile();
nRec++;
}
}*/
Customer Billing System in C using Files
#include<stdio.h>
#include<string.h>
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);
}
Recent Comments