In the file ‘CUSTOMER.DAT’ there are 100 records with the following structure:
struct customer
{
int accno ;
char name[30] ;
float balance ;
} ;
In another file ‘TRANSACTIONS.DAT’ there are several records with the following structure:
struct trans
{
int accno ;
char trans_type ;
float amount ;
} ;
The parameter trans_type contains D/W indicating deposit or withdrawal of amount. Write a program to update ‘CUSTOMER.DAT’ file, i.e. if the trans_type is ‘D’ then update the balance of ‘CUSTOMER.DAT’ by adding amount to balance for the corresponding accno. Similarly, if trans_type is ‘W’ then subtract the amount from balance. However, while subtracting the amount make sure that the amount should not get overdrawn, i.e. at least 100 Rs. Should remain in the account.
Create a folder "ctxt" in C drive. In the "ctxt" folder create two text files with name cust.txt and trans.txt.
when option 4 is executed trans.txt get deleted while new transactions can be created with option 5 in program.
#include "stdio.h"
struct customer
{
int accno;
char name[30];
float balance;
};
struct trans
{
int accno;
char trans_type[1];
float amount;
};
void main()
{
char another,name1[30];
FILE *fs,*ft,*ftemp;
struct customer c;
struct trans t;
int choice,no,f2,no2,counter;
long pos1,pos2,pos3;
fs=fopen("c:\\ctxt\\cust.txt","r+");
if(fs==NULL)
{
puts("Cannot open file");
exit();
}
while(1)
{
clrscr();
printf("1)List Customer Data");
printf("\n2)Add Customer Data");
printf("\n3)Remove Customer Data");
printf("\n4)Update Customer Data from transactions");
printf("\n5)Perform transactions");
printf("\n6)Exit\n");
scanf("%d",&choice);
rewind(fs);
switch(choice)
{
case 1:
another='y';
clrscr();
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
printf("\nAccount no: %3d Name: %-20s Balance: %.1f",c.accno,c.name,c.balance);
while(1)
{
printf("\n\nHit enter to escape");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;
case 2:
clrscr();
another='y';
no=1;
funcsort(fs);
while(1)
{
f2=0;
for(no=1;fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF;no++)
{
if(f2==0)
{
if(c.accno!=no)
{
no2=no;
f2=1;
}
}
}
printf("\nKey in Customer name\n");
scanf("%s",c.name);
printf("\nKey in balance\n");
scanf("%f",&c.balance);
if(f2==1)
{
fprintf(fs,"%d %s %f\n",no2,c.name,c.balance);
}
else
{
fprintf(fs,"%d %s %f\n",no,c.name,c.balance);
no++;
}
funcsort(fs);
printf("\nAdd another customer Data(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;
case 3:
another='y';
clrscr();
while(1)
{
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
printf("\nKey in name to delete\n");
scanf("%s",name1);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
if(strcmp(c.name,name1)!=0)
fprintf(ftemp,"%d %s %f\n",c.accno,c.name,c.balance);
}
fclose(fs);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
printf("\nDelete another data?(y/n)\n");
fflush(stdin);
fs=fopen("c:\\ctxt\\cust.txt","r+");
another=getche();
if(another!='y')
break;
}
break;
case 4:
clrscr();
ft=fopen("c:\\ctxt\\trans.txt","r");
if(ft==NULL)
{
puts("No active transactions");
exit();
}
while(fscanf(ft,"%d%s%f",&t.accno,t.trans_type,&t.amount)!=EOF)
{
counter=0;
pos1=0;
rewind(fs);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
counter++;
pos2=pos1;
pos1=ftell(fs);
pos3=pos1-pos2;
if(t.accno==c.accno)
{
if(t.trans_type[0]=='w')
{
c.balance-=t.amount;
if(c.balance<100)
{
c.balance+=t.amount;
printf("\nTransaction for account no: %d not processed due to insufficient funds");
}
}
if(t.trans_type[0]=='d')
{
c.balance+=t.amount;
}
if(counter==1)
fseek(fs,-(pos3),SEEK_CUR);
if(counter>1)
fseek(fs,-(pos3-2),SEEK_CUR);
fprintf(fs,"%d %s %f\n",c.accno,c.name,c.balance);
counter=1;
}
}
}
fclose(ft);
remove("c:\\ctxt\\trans.txt");
break;
case 5:
clrscr();
another='y';
ft=fopen("c:\\ctxt\\trans.txt","a+");
if(ft==NULL)
{
puts("Cannot open file");
break;
}
while(1)
{
printf("\nKey in acc no.\n");
scanf("%d",&t.accno);
printf("\nWithdraw (w) or Deposit (d)\n");
scanf("%s",t.trans_type);
printf("\nKey in amount\n");
scanf("%f",&t.amount);
fprintf(ft,"%-3d %s %-6.2f\n",t.accno,t.trans_type,t.amount);
printf("\nDo you want to key another transaction(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
fclose(ft);
break;
case 6:
fclose(fs);
exit();
}
}
fclose(fs);
}
funcsort(FILE *fp)
{
char ch;
int flag=0,i,j,count;
struct customer a[1000],temp1,temp2;
FILE *ftemp;
rewind(fp);
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
for(i=0,count=0;i<1000;i++)
{
if(flag==0)
{
ch=fscanf(fp,"%d%s%f",&a[i].accno,a[i].name,&a[i].balance);
count++;
if(ch==EOF)
{
flag=1;
}
}
if(flag==1)
{
a[i].name[0]=0;
}
}
for(i=0;a[i].name[0]!=0;i++)
{
for(j=i+1;j<(count-1);j++)
{
if(a[i].accno>a[j].accno)
{
temp1=a[i];
temp2=a[j];
a[i]=temp2;
a[j]=temp1;
}
}
}
for(i=0;a[i].name[0]!=0;i++)
{
fprintf(ftemp,"%d %s %f\n",a[i].accno,a[i].name,a[i].balance);
}
fclose(fp);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
fp=fopen("c:\\ctxt\\cust.txt","r+");
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment