Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
struct record
{char des[26],num[10];
float price;
}trans;
FILE *f1,*f2;
void main()
{ void add(struct record);
void edit(struct record);
void del(struct record);
char ans;
clrscr();
do{
printf("[A]dd\n");
printf("[C]hange\n");
printf("[D]elete\n");
printf("E[x]it\n");
printf("Choose: ");
ans=getche();
printf("\n");
switch(toupper(ans))
{
case 'A':add(trans);break;
case 'C': edit(trans);break;
case 'D': del(trans);break;
default: printf("Invalid Input\n");break;
}}
while (toupper(ans)!='X');
getch();
}
void add (struct record trans)
{clrscr();
char ans;
f1=fopen("record.txt","a");
do
{
printf("Part Number:\n");
scanf("%s",trans.num);
printf("Part Description:\n ");
scanf("\n");
gets(trans.des);
printf("Price:\t");
scanf("%f",&trans.price);
fprintf(f1,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
printf("Another record? Y/N: ");
ans=getche();
}while(toupper(ans)=='Y');
fclose(f1);
}
void edit(struct record trans)
{char neym[20],ans,code;
int g;
printf("Enter number to be edited: ");
scanf("%s",neym);
f1=fopen("record.txt","r");
f2=fopen("record1.txt","w");
printf("\nChange code P or D:\t");
code=getche();
if(toupper(code)=='D')
{
while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
{
if(strcmpi(neym,trans.num)==0)
{
printf("Enter new description :");
scanf("\n");
gets(trans.des);
strcpy(trans.num,neym);
trans.price=trans.price;
}
fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
}
}
if(toupper(code)=='P')
{
while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
{
if(strcmpi(neym,trans.num)==0)
{
printf("Enter new price :");
scanf("%f",&trans.price);
}
fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
}
}
remove("record.txt");
fclose(f1);
fclose(f2);
rename("record1.txt","record.txt");
}
void del(struct record trans)
{char neym[20];
f1=fopen("record.txt","r");
f2=fopen("record1.txt","w");
printf("Enter part number:\t ");
scanf("%s",neym);
while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
{if(strcmpi(neym,trans.num)!=0)
fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
}
remove("record.txt");
fclose(f1);
fclose(f2);
rename("record1.txt","record.txt");
}